I’m using a custom query on a page after the initial loop to list some posts from a category I’m associating with that page. Im doing this:
query_posts(array(
'category_name' => $name,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1,
'paged' => $paged));
I’ve also tried using get_query_var('paged') in place of $paged btw.
then I do my loop and it shows the correct number of posts, 1. I actually am getting links generating on the page if I use previous_posts_link() or next_posts_link(), however, if I use echo paginate_links() after or even in my loop, no links are generated and where as I have 2 posts in that category currently so I expect to see at least a link to page 2. It seems like there is a lot of info out there about passing the correct info to query_posts for this instance but not what function to use to actually render the pagination links so i’d appreciate any insight. Thanks.
Edit:(full code)
<div id="left_column_wrap">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post" id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php //include (TEMPLATEPATH . '/_/inc/meta.php' ); ?>
<div class="entry">
<?php the_content(); ?>
<?php //wp_link_pages(array('before' => 'Pages: ', 'next_or_number' => 'number')); ?>
</div>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</article>
<?php endwhile; endif;
$name = '';
if(isset($pagename))
{
$name = $pagename;
}
if($name == '5a' || $name == '4a' || $name == '3a' || $name == '2a')
{
echo '<div id="all_cats_recent_posts_wrap">';
query_posts(array('category_name' => $name, 'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => 1, 'paged' => $paged));
if (have_posts()):
while (have_posts()):
the_post(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="nofollow noreferrer noopener"><?php the_title(); ?></a></h2>
<?php include (TEMPLATEPATH . '/_/inc/meta.php' ); ?>
<div class="entry">
<?php echo substr(strip_tags(get_the_content()), 0, 280).'...';?>
<a class="quick_link" href="<? the_permalink()?>" rel="nofollow noreferrer noopener">(read more...)</a>
</div>
</article>
<?
endwhile;
echo paginate_links();
?>
<div class="alignleft"><?php previous_posts_link('« Previous') ?></div>
<div class="alignright"><?php next_posts_link('More »') ?></div>
<?
endif;
echo '</div>';
}
?>
</div>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Though all of the arguments are optional, paginate_links doesn’t necessarily do anything if there are no arguments. Take a look at the example at the example in the Codex.
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages
) );
That works. Try it. Now remove the last parameter. See? Try passing that total to your function.
Again, please don’t use query_posts, especially for secondary loops. Use get_posts or create a new WP_Query object. I’d do the latter. I don’t think you’d really need to alter much.
$myquery = new WP_Query(
array(
'category_name' => $name,
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 1,
'paged' => $paged
)
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0