• Resolved lingo123

    (@lingo123)


    I am very new to WordPress and need help paginating query results. The site I manage has a “News” category, and has been coded (by a techie who is no longer on the team so I can’t ask him) such that when a visitor clicks “Get all news,” all the news posts show up on one page.

    We have over 100 posts now and the list grows daily.

    I would like the posts list paginated so that there are maybe 20 posts per page, with a link to next page/previous page.

    I am clueless.

    Here’s the coding we have for catergory-news.php:

    <?php get_header(); ?>

    <div id=”newspagecolumn”>
    <h1>News</h1>
    <?php

    $postcount = 0;

    $featured_query = new WP_Query(‘category_name=News’);

    while ($featured_query->have_posts()) : $featured_query->the_post();

    $do_not_duplicate[] = get_the_ID();

    $postcount++;

    ?>
    <div id=”newspage”>
    <span class=”newspageheader”>“><?php the_title(); ?></span>
    <div id=”newsdate”><span class=”newsmonth”><?php the_time(‘M’) ?></span> <span class=”newsday”><?php the_time(‘d’) ?></span></div>
    <div class=”newspagecontent”><?php the_excerpt(); ?></div><div class=”readmore”>“>read more…</div></div>

    <!– End Panel –>

    <?php endwhile; ?></div>

    <?php get_footer(); ?>

    Please, somebody, tell me where I put the $paged stuff? And how exactly does it need to read? (And please forgive me if this is covered elsewhere–I’ve been researching for days but can’t seem to find an answer that makes sense to me, WordPress n00b that I am…)

Viewing 5 replies - 1 through 5 (of 5 total)
  • I have seen reports of problems with paging and category queries, but this is how you would use the $paged variable. Change this:

    $postcount = 0;
    
    $featured_query = new WP_Query('category_name=News');

    to this:

    $postcount = 0;
    
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $featured_query = new WP_Query("category_name=News&posts_per_page=20&paged=$paged");

    Note the use of double quotes around the WP_Query parameters.

    Thread Starter lingo123

    (@lingo123)

    Thank you so much! OK, I’ve tried that, and it does indeed put 20 posts on a page.

    But now I need to know what to add to the code so that it will create clickable links that say something simple like “Page 1 2 3” …? at the bottom of the 20 posts. Because otherwise we just get a page of 20 posts and that looks like that’s all there are.

    Thank you again! Sorry to be such a n00b….

    Don’t apologize for being new. Everybody is at some time.

    The exact code and its placement depends on your theme. Without access to the theme, I can only give general suggestions. In the ‘old’ default theme, the code is this:

    <div class="navigation">
       <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
       <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    </div>

    It is placed just after the end of the ‘while’ loop.

    You might look for similar code (containing next_posts_link, for example) in other modules in your theme to get exact syntax.

    Thread Starter lingo123

    (@lingo123)

    Thank you for your continued help and patience. I added the code to the page in various places (because I’m not sure if you mean it goes after the “<?php endwhile; ?></div>” line…?)

    The theme we’re using was customized from Twenty Ten 1.1. I can’t find next_posts_link used anywhere in any of the templates that were created for the site.

    This is driving me crazy. Why is it so difficult to just get WordPress to display results in a paginated list of posts with links to Pages 1, 2, 3, etc.? For now I’m still stuck listing all 112 posts (and counting) on one page so that visitors don’t miss anything…

    Thread Starter lingo123

    (@lingo123)

    OH HEY! I got it to work!

    Here was my mistake: I needed to change the settings under “Reading.” The “Blog pages show at most XX posts” had been set to 10000000 (I kid you not). I changed it to match the “20” in the coding above, and voila! I have query result pagination PLUS nav links!

    Thank you again for all your help!

    (Now if I could just get the navlinks to display at the BOTTOM of the pages instead of at the top… but Imma go with this for now because IT’S WORKING!)

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘New user needs help with $paged variable’ is closed to new replies.