WordPress Pagination Problem

I want to put Pagination in my wordpress index.php but it does not work. In my index.php there are 3 section of loop that show custom posts and i want put pagination in Blog / recent post section loop in index.php. this is the code in index.php

<?php
/**
* @package WordPress
* @subpackage Adapt Theme
*/
$options = get_option( 'adapt_theme_settings' );
?>
<?php get_header(); ?>

<div class="home-wrap clearfix">

<!-- Homepage tagline -->
<?php if(get_bloginfo('description')) { ?>
<aside id="home-tagline">
    <?php echo bloginfo('description'); ?>
</aside>
<!-- /home-tagline -->
<?php } ?>

<!-- /Homepage Slider -->
<?php get_template_part( 'includes/slides' ); ?> 

<!-- Homepage Highlights -->
<?php
//get post type ==> hp highlights
    global $post;
    $args = array(
        'post_type' =>'hp_highlights',
        'numberposts' => '-1'
    );
    $highlight_posts = get_posts($args);
?>
<?php if($highlight_posts) { ?>        


<section id="home-highlights" class="clearfix">
    <?php
    $count=0;
    foreach($highlight_posts as $post) : setup_postdata($post);
    $count++;
    //get img
    $feat_img = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full-size');
    //meta
    $highlights_url = get_post_meta($post->ID, 'adapt_highlights_url', TRUE);
    ?>

    <article class="hp-highlight <?php if($count == '4') { echo 'remove-margin'; }   if($count == '3') { echo ' responsive-clear'; } ?>">
        <h2>
        <?php if($feat_img) { ?><span><img src="https://wordpress.stackexchange.com/questions/72559/<?php echo $feat_img[0]; ?>" alt="<?php the_title(); ?>" /></span><?php } ?>
        <?php if($highlights_url) { ?>
            <a href="<?php echo $highlights_url; ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php } else { the_title(); } ?>
        </h2>

        <?php the_excerpt(); ?>
    </article>
    <!-- /hp-highlight -->

    <?php
    if($count == '4') { echo '<div class="clear"></div>'; $count=0; }
    endforeach; ?>
</section>
<!-- /home-projects -->         
<?php } ?>


<!-- Recent Portfolio Items -->
<?php
//get post type ==> portfolio
    global $post;
    $args = array(
        'post_type' =>'portfolio',
        'numberposts' => '4'
    );
    $portfolio_posts = get_posts($args);
?>
<?php if($portfolio_posts) { ?>        
    <section id="home-projects" class="clearfix">
        <h2 class="heading"><span><?php if(!empty($options['recent_work_text'])) { echo $options['recent_work_text']; } else { _e('Recent Work','adapt'); }?></span></h2>

        <?php
        $count=0;
        foreach($portfolio_posts as $post) : setup_postdata($post);
        $count++;
        //get portfolio thumbnail
        $feat_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'grid-thumb');
        ?>

        <?php if ($feat_img) {  ?>
        <div class="portfolio-item <?php if($count == '4') { echo 'remove-margin'; } if($count == '3') { echo ' responsive-clear'; } ?>">
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="https://wordpress.stackexchange.com/questions/72559/<?php echo $feat_img[0]; ?>" height="<?php echo $feat_img[2]; ?>" width="<?php echo $feat_img[1]; ?>" alt="<?php echo the_title(); ?>" />
            <div class="portfolio-overlay"><h3><?php echo the_title(); ?></h3></div><!-- portfolio-overlay -->
            </a>
        </div>
        <!-- /portfolio-item -->
        <?php } ?>

        <?php
        if($count == '4') { echo '<div class="clear"></div>'; $count=0; }
        endforeach; ?>
    </section>
    <!-- /home-projects -->         
<?php } ?>


<!-- Recent Blog Posts -->
<?php
//get post type ==> regular posts
    global $post;
    $args = array(
        'post_type' =>'post',
        'numberposts' => '12'
    );
    $blog_posts = get_posts($args);
?>
<?php if($blog_posts) { ?>        
    <section id="home-posts" class="clearfix">
        <h2 class="heading"><span><?php if(!empty($options['recent_work_text'])) { echo $options['recent_news_text']; } else { _e('Recent News','adapt'); }?></span></h2>
        <?php
        $count=0;
        foreach($blog_posts as $post) : setup_postdata($post);
        $count++;
        //get portfolio thumbnail
        $feat_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'grid-thumb');
        ?>


        <article class="home-entry <?php if($count == '4') { echo 'remove-margin'; } if($count == '3') { echo ' responsive-clear'; } ?>">
            <div class="home-entry-image">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><img src="<?php echo catch_that_image() ?>"></a>
    </div>
            <div class="home-entry-description">
                <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo the_title(); ?></a></h3>
                <?php echo excerpt('15'); ?>
            </div> 
            <!-- /home-entry-description -->
        </article>
        <!-- /home-entry-->
        <?php
        if($count == '4') { echo '<div class="clear"></div>'; $count=0; }
        endforeach; ?>
    </section>
    <!-- /home-posts -->
<!-- AREA WHERE I WANT PUT PAGINATE -->
<?php } ?>

</div>
<!-- END home-wrap -->   
<?php get_footer(); ?>

The area that i want put paginate is in “AREA WHERE I WANT PUT PAGINATE” (you can see comment tag above), but it is not working, if i click page 2, it always shows the posts of page one (not change).

Here is code for file paginate.php that include in my function file

<?php
/**
 * @package WordPress
* @subpackage Adapt Theme
*/

//pagination function
function pagination($pages="", $range = 4)
{
 $showitems = ($range * 2)+1; 

 global $paged;
 if(empty($paged)) $paged = 1;

 if($pages == '')
 {
     global $wp_query;
     $pages = $wp_query->max_num_pages;
     if(!$pages)
     {
         $pages = 1;
     }
 }  

 if(1 != $pages)
 {
     echo "<div class=\"pagination clearfix\">";
     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href="".get_pagenum_link(1)."">&laquo;</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href="".get_pagenum_link($paged - 1)."">&lsaquo;</a>";

     for ($i=1; $i <= $pages; $i++)
     {
         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
         {
             echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href="".get_pagenum_link($i)."" class=\"inactive\">".$i."</a>";
         }
     }

     if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">&rsaquo;</a>";
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href="".get_pagenum_link($pages)."">&raquo;</a>";
     echo "</div>\n";
 }
}
?>

Note: I using template “Adapt” wordpress theme from wpexplorer.me.

1 Answer
1

When creating custom lists of posts pagination won’t happen automatically, you’ll need to pass the pagination arguments into your get_posts call. The full details can be found on the WP_Query codex page.

For the query you want to be paged you’ll need to add something like 'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1; to your $args. And if I am reading correctly I don’t see that you are actually calling the pagination display function in your template, you should be able to add <?php pagination(); ?> where you have <!-- AREA WHERE I WANT PUT PAGINATE --> [sic].

There may be some documentation on using this on the theme developers website, I would suggest reading more about it there, potentially there is even an example you can copy.

Leave a Comment