3

I just created a new custom page template and called two posts only. How can I add pagination so that I can the link with at least two of the lastest posts?

I tried this code but it does not work:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'category' => 1,
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post();?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt();?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo</a>
                </div>
            </div>   
        <?endwhile; 
        // Reset Query
        wp_reset_query();
    ?>

Any help?

Sidney Sousa
  • 53
  • 1
  • 6

2 Answers2

2

Since you're using "the loop" you should use the built in function for displaying pagination.

Here's some examples for you: https://codex.wordpress.org/Pagination

I've updated your sample code to show the default pagination:

<?php
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    query_posts( 
        array ( 
            'post_type' => 'post', 
            'category_name' => 'news', 
            'posts_per_page' => 2,
            'paged' => $paged ) 
        );      
        // The Loop
        while ( have_posts() ) : the_post(); ?>
            <div class="news-page-content-wrapper">
                <div class="news-page-content">
                    <h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                    <figure><?php the_post_thumbnail(); ?></figure>
                    <p><?php echo get_the_excerpt(); ?></p>
                    <a href="<?php the_permalink(); ?>">Read More&raquo; </a>
                </div>
            </div>   
        <?php endwhile; 

        the_post_navigation();
        // Reset Query
        wp_reset_query();
    ?>
Daniel
  • 2,167
  • 5
  • 23
  • 44
0

I sill faced that issue and solved it like that

Pagination function

function post_pagination($paged = '', $max_page = '') {
    if (!$paged) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
    }

    if (!$max_page) {
        global $wp_query;
        $max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
    }

    $big  = 999999999; // need an unlikely integer

    $html = paginate_links(array(
        'base'       => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
        'format'     => '?paged=%#%',
        'current'    => max(1, $paged),
        'total'      => $max_page,
        'mid_size'   => 1,
        'prev_text'  => __('« Prev'),
        'next_text'  => __('Next »'),
    ));

    $html = "<div class='navigation pagination'>" . $html . "</div>";

    echo $html;
}

Query on the custom template

$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => get_option('posts_per_page'),
    'order' => 'DESC',
    'orderby' => 'date',
    'paged' => $paged
);

$loop = new WP_Query($args);
if ($loop->have_posts()) :
    while ($loop->have_posts()) :
        $loop->the_post();
        get_template_part('content', get_post_format());
    endwhile;

    post_pagination($paged, $loop->max_num_pages);
endif;

wp_reset_postdata();