1

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() ?>"><?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()?>">(read more...)</a>                    
                </div>
            </article>
            <?
            endwhile;
            echo paginate_links();
            ?>
            <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
            <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
            <?
        endif;
        echo '</div>';

    }
    ?>
</div>
2
  • 1
    You should really not be using query_posts. Create a new WP_Query object instead. Please post all the code related to attempt at paginate_links. I am guessing you are using it for something it isn't intended for, but post the code and let's see.
    – s_ha_dum
    Commented Dec 28, 2012 at 18:01
  • @s_ha_dum added the full page template code. Note I'm trying to do the pagination on the second loop.
    – John B
    Commented Dec 28, 2012 at 18:17

1 Answer 1

4

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
    )
);
1
  • I'm using pretty permalinks so don't I have to change the base and format args of paginate links? The codex doesn't really give an example...(edit) wait never mind I guess thats what htaccess is for....
    – John B
    Commented Dec 28, 2012 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.