Pagination in a template page?
-
I created a template as follows:
<?php
/*
Template Name: Test
*/
?>
<?php get_header(); ?>
<?php $my_query = new WP_Query('category_name=Post&paged='.$q['paged']); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h5><a href="<?php the_permalink()?>" class="posttitle"><?php the_title(); ?></a></span></h5>
By <?php the_author()?>, on <?php the_time('M jS, y')?>
<?php the_excerpt(); ?>
<a>">Continue reading...</a>
<?php endwhile; ?>
<?php get_nav_link(7) ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I create a custom page based on this template and want that a limited number of posts appear on this page and pagination links appear at bottom.
The page works fine when I call the page as https://localhost/wordpress/?page_id=77&paged= or https://localhost/wordpress/?page_id=77&paged=1 but when I pass the value of page as 2 as in https://localhost/wordpress/?page_id=77&paged=2, the index.php page comes into play (though there are more posts in the category ‘Post’).
Can anyone please help me with what is preventing the pagination?
PS. get_nav_link() is a customized function based on the posts_nav_link() function and accepts the category code as parameter.
function get_nav_link($cat, $sep=' — ', $prelabel='? Previous Page', $nxtlabel='Next Page ?') {
global $request, $posts_per_page, $wpdb, $max_num_pages;
if ( !is_single() ) {
if ( 'posts' == get_query_var('what_to_show') ) {
if ( !isset($max_num_pages) ) {
$numposts = $wpdb->get_var("SELECT COUNT( DISTINCT a.ID ) FROM wp_posts a, wp_post2cat b WHERE a.ID = b.post_id AND b.category_id = $cat");
$max_num_pages = ceil($numposts / $posts_per_page);}
}
else {
$max_num_pages = 999999;
}if ( $max_num_pages > 1 ) {
previous_posts_link($prelabel);
echo preg_replace('/&([^#])(?![a-z]{1,8};)/', '&$1', $sep);
next_posts_link($nxtlabel, $max_page);
}
}
}
- The topic ‘Pagination in a template page?’ is closed to new replies.