• leepr

    (@leepr)


    Hi there,

    I currently have a WP installation with 4 main categories. For reasons I won’t go into, I only want to display posts from a specific category on the blog / main index template. I can accomplish that no problem, but am having problems creating the next/previous post links at the bottom.

    Here are a couple of flawed solutions explaining what I’m trying to do, and the reasons why they fail:

    Method 1:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post();
       // If the post is in category 4...
       // <snip> - display the post, etc.
    ?>
    
    <!--navigation-->
    <?php next_posts_link('&laquo; Previous Entries') ?>
    <?php previous_posts_link('Next Entries &raquo;') ?>

    This will work for the most part, but if I’m on the last post under category 4, it will still display a ‘previous entries’ link if there’s a post before it in another category.

    Method 2:

    <?php if (have_posts()) : ?>
    <?php query_posts('cat=4');
       // <snip> - display the post, etc.
    ?>
    
    <!--navigation-->
    <?php next_posts_link('&laquo; Previous Entries') ?>
    <?php previous_posts_link('Next Entries &raquo;') ?>

    This looks like it’ll work on first glance, but because of the call to query_posts('cat=4'); it will always display the first 2 posts under that category (blog set to show only 2 posts per page). I could add parameters to it e.g. `showposts=2&offset=1, but I can’t add params like that to next_post_link or previous_post_link.

    What is the smartest way to accomplish displaying only posts from a certain category, and having accurate navigation to navigate between the next/previous entries at the bottom?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I think you’re missing the “;” after the closing brackets.

    Corrected code:

    <?php next_posts_link('&laquo; Previous Entries')<strong>;</strong> ?>
    <?php previous_posts_link('Next Entries &raquo;')<strong>;</strong> ?>

    i don’t know why, but the bold highlight didn’t work….jus ignore the for the ;

    soz lol

    <?php next_posts_link(‘« Previous Entries’); ?>
    <?php previous_posts_link(‘Next Entries »’); ?>

    Ok, this has bothered me for a long time and now i have finally found a solution on a website.

    Where’s the problem? query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is.

    How to fix it? To get proper pagination with query_posts() we need to recreate it through the ‘paged’ parameter or query. Best way to do this is to ask WordPress for the “page” we happen to be on, and use that as our ‘paged’ value. There’s the code for it

    <?php if (have_posts()) : ?>
    <?php query_posts(“category_name=somecat”); ?>
    <?php while (have_posts()) : the_post(); ?>

    replace with

    <?php if (have_posts()) : ?>
    <?php $paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1; query_posts(“category_name=somecat&paged=$paged”); ?>
    <?php while (have_posts()) : the_post(); ?>

    And that’s it! The $paged = line above uses what’s called a ternary operator to test whether the ‘paged’ query variable is available. If it is, $paged receives its value. If it isn’t, we assume we’re on the first page and assign it ‘1’. Then we tell query_posts() what page we’re on with the addition of &paged=$paged.

    Hey, I had a problem like this as well, but was able to work out a solution using this tip! It is a great solution for custom query pages and navigation. Thanks for the tip!

    In my install (2.7.1), I had to place the $paged variable outside of the query_posts statement, like this:

    <?php if (have_posts()) : ?>
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts($query . "category_name=somecat&paged=" . $paged); ?>
    <?php while (have_posts()) : the_post(); ?>
    Bongo

    (@jbudisantoso)

    thanks for the post rizwanashraf. worked like a charm.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Displaying posts from specific categories & next/previous_posts_link()’ is closed to new replies.