Issue
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»</a>
</div>
</div>
<?endwhile;
// Reset Query
wp_reset_query();
?>
Any help?
Solution
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» </a>
</div>
</div>
<?php endwhile;
the_post_navigation();
// Reset Query
wp_reset_query();
?>
Answered By - Daniel
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.