Support » Fixing WordPress » Is there a smart way to use Next/Previous post button?

  • I have a Next/Previous button on my blog, which works great when viewing from the homepage and going backwards.

    <div><h4><span><?php next_post('% &lsaquo;&lsaquo;&lsaquo;',  'Previous', 'no', 'no'); ?></span>
     <span> <div class="right"><?php previous_post('&rsaquo;&rsaquo;&rsaquo;  %', 'Next', 'no', 'no'); ?></span>

    However, every post is split into categories and I have a menu for every category…this is when i run into problems. I use the above code in the single.php, but when you click on a post within its category and click next/previous…it uses the next or previous post using time…not category.

    Is it possible to use time on my index/single and category within cat/single?

    Does that makes sense?

Viewing 15 replies - 1 through 15 (of 16 total)
  • Maybe this explains it better:
    Is it possible to do the following: if the user visits and continues browsing normally (through the posts have I have posted them), the next/previous buttons work as normal, but if user is browsing via a category, the next and previous buttons will know this and take the user through the rest of those post and not the actual post date order?

    I don’t think WordPress supports this; when you’re visiting a post through single.php, it’ll behave the same regardless of how the user got there (either by category or by listing).

    What i’d suggest you to do is a add a parameter to your links pointing to posts from category listings so that in your single.php you can catch this URL parameter (using $_GET[]) and handle it appropriately.

    Here’s a quick example:

    <a href="<?php the_permalink($post->id);?>?view=category">link to post form category listing</a>

    Now in your single.php you simply have to check:

    if(isset($_GET['view']) && $_GET['view'] == 'category'){
    //handle your links as if they came from the category listing
    //handle your links as if they came form the homepage or from another listing

    Within that conditional structure, i’d suggest you write custom queries to get the next and previous posts so you have better control on what post (and it’s type) is being retrieved for you. Unfortunately you won’t be able to use next_post to get the next category post and same thing for the previous post.

    I have been trying to get my head round this since you posted. I use this to display the categories on my index.php, which links to the category page:

    // For each of the categories, create them as category variables
    foreach((get_the_category()) as $category) {
        // If the category is NOT the cat_name (category name) then echo some stuff
        // So grab all... except where they match cat_name (exclude basically)
        if ($category->cat_name != 'Featured Small') {
        echo '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>[' . $category->name.']</a> ';
    //added [ = [ and ] = ]

    As you can tell, somebody wrote that code for me…In new to the world of php.

    So i amend that with <a href="<?php the_permalink($post->id);?>?view=category">link to post form category listing</a>

    I think it can be done…but it will be better if a custom child theme is created, so you can basically create special category template with all links to posts or posts. It was easier if you wanted a category page with a list linking to posts.

    In that case, this plugin (actually for creating both human readable and xml sitemap) –

    might have worked if you actually wanted a list (not full post).

    Install the plugin and go to the options page to understand what I want to say – it has options to include exclude cats plus it support loops :

    <?php echo ddsg_create_sitemap(); ?>

    If you set “Items per page = 0” in options page, there will be no pagination.

    If you tweak the plugin, it might work, if you are desperate to get you can ask him if here is no further idea.

    Hi Abhishek, thanks for your input. I will install that plugin and mess around with it. Im still trying to firgue out the method that patv talked about, maybe a plugin would be an easier route for me to go down.

    Cheers guys, all help welcome ๐Ÿ™‚

    I’ll try to explain better.

    Specifically to your single.php, you want your previous/next button to navigate to your next or previous post:
    1- of the same category if you navigated to that post through category.php.
    2- of all the posts if you navigated to that post through index.php.

    In this case, in your category.php, within your Loop when you iterate through all your category posts, append ‘?viewport=category’ to the end of the anchor’s href. Here’s a quick example:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <a href="<?php the_permalink();?>?viewpost=category"><h2><?php the_title();?></h2></a>
    <?php the_content();?>
    <?php endwhile; endif; ?>

    Add this to your category.php file. As you can see, i’m appending a parameter to the link for each post within the loop.

    You can use the same code in your index.php, just don’t append anything to the posts’ URLs.

    No, in your single.php, you’ll detect this variable using the $_GET variable and handle the next and previous links appropriately:

    if(isset($_GET['viewport']) && $_GET['viewport'] == 'category'){
    //handle your links as if they came from the category listing
    //handle your links as if they came form the homepage or from another listing

    That’s the logic behind it. To grab the next and previous posts form a specific category, you’ll need to create your own query that filters for the appropriate category. Then, from the array of posts, find the first post that is chronically before the current post and the same for the next post. Extract their links and include these into the next and previous buttons. If no ‘viewport’ variable is detected, then revert to WPs built-in next/previous buttons since you don’t want to limit the scope to one category specifically.

    Does this makes sense to you?

    Hi again patv, thanks for checking back. Im glad you did, your a php wizard ๐Ÿ™‚

    Ok, I added ‘?viewport=category’ to my category.php and noticed the urls change from

    To this

    Which looks promising! Im now trying to add your $_GET command to my next/previous buttons. Just to clarify, the custom query should go where you have //handle your links? No success yet, but I will keep trying till I get this…finishing work soon so will try again when I get home.

    Cheers again, its very much appreciated.

    So the script you mentionned goes into your category.php. When this conditional check is true, then you’ll use your custom query. When the conditional check is false, use WordPress’ previous/next buttons.

    Patv, sorry but Im strugging the with setting up a custom query and how to combine it all. I hope its not too much to ask, but could I post up the category/Single.php here and you see what you think?

    Of course.

    I was going to, but deleted it (post that was before your most current reply). Wanted to wait and see if you replied…which you did ๐Ÿ™‚

    Ok, so here it goes, category with viewpost=category

          global $wp_query;
          $args = array(
                       'post_type' => 'post',
                       'posts_per_page' => 18,
                       'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
          $final_args = array_merge( $wp_query->query, $args );
          $x = 0;
          while (have_posts()) : the_post(); ?>
            <?php if($x == 0) { ?>
            <div class="featured_box first">
            <?php } elseif($x == 5) { ?>
            <div class="featured_box last">
            <?php } else { ?>
            <div class="featured_box">
            <?php } ?>
                       <a>"><?php tern_wp_youtube_image(); ?>          </a><a>"><?php the_post_thumbnail('featured-small'); ?></a>
              <h3><a>?viewpost=category"><?php the_title(); ?></a></h3>

    Single.php, this is where I got confused:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <?php if($single) { ?>
     <?php } ?>
           <p> <?php the_content(); ?></p>  <br /><br />
     <?php if(isset($_GET['viewport']) && $_GET['viewport'] == 'category'){
    //handle your links as if they came from the category listing
    //handle your links as if they came form the homepage or from another listing
    } ?>
    <div><h4><span><?php next_post('% โ€นโ€นโ€น',  'Previous', 'no', 'no'); ?></span>
     <span> <div class="right"><?php previous_post('โ€บโ€บโ€บ  %', 'Next', 'no', 'no'); ?></span>
            <br /><br />

    It still works, but Im guessing its single.php…

    If you have a website with paypal (or a personal paypal acc?) I can make a donation as I know your going out your way here.

    Please use or as the thread is becoming longer and for future reference it will look clean.

    Kindly do not offer monetary rewards on WordPress support forum (it is against the rules) openly, instead contact the intended person personally from profile link or post in WP Jobs (link at footer).

    Hi Abhishek, sorry I will use the websites you mentioned for future code/questions. I couldnt see a pm funtion or webite/email for patv…will again will keep that in mind for the future. Thanks for letting me know.

    fracmo2000, shoot me your email and we’ll debug this together. As Abhishek Ghosh mentionned, this thread is becoming too long. What we’ll do it submit the final solution on this thread, but in the meantime we’ll figure this out through emails.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Is there a smart way to use Next/Previous post button?’ is closed to new replies.