Support » Fixing WordPress » previous_post_link AND next_post_link – browse ONLY in same category

  • I have the following scenario:

    I’m reading a post which is connected to one category.

    In this post im able to browse previous and next post.

    My problem is, that when i’ve browsed the few post in this category, the browse will go on to post from other categories.

    Am I able to ONLY browse the post from the category which the post belongs to.

    My browsing links looks like the following:

    <div id="nav-above" class="navigation grid_16">
    					<div class="nav-previous"><?php previous_post_link( '%link', '<div class="meta-nav">' . _x( '&laquo;', 'Previous post link', 'twentyten' ) . ' %title</div> ' ); ?></div>
    					<div class="nav-next"><?php next_post_link( '%link', '<div class="meta-nav">%title ' . _x( '&raquo;', 'Next post link', 'twentyten' ) . '</div>' ); ?></div>
    				</div>
Viewing 15 replies - 1 through 15 (of 18 total)
  • This Stay In Category article might have an answer for you.

    As shown in the documentations for ‘previous_post_link’ and ‘next_post_link’ both functions can take four parameters:

    $format
    $link
    $in_same_cat
    $excluded_categories

    As you should be able to guess you want to set the third one, (which excepts a boolean of either true or false) to false, which will allow your to browse irrespective of category.

    <?php previous_post_link( '%link', '<div class="meta-nav"> &larr; %title</div>', false ); ?>

    previous_post_link and next_post_link

    @jason: As explained in the article I linked, the in_same_cat parameter works as long as each post is assigned to only a single category.

    If posts are assigned to multiple categories, then the previous/next_post_link functions will not stay in the original category, but only one of the group.

    great tip thank you very much.

    @vtxyzzy: I’m glad you clarified that. I was having the same problem.

    Do you know of how this would work when the posts are assigned to multiple categories? Or if it can be done at all? I’ve seen other people post similar questions in the forums, but with no response.

    The article I originally linked to (Stay In Category) has one possible solution.

    I see where you’re going with that, and it looks like it does exactly what I need. However, I’m at the part where the tutorial calls for adding code to my functions.php file.

    I’m getting an “unexpected T_STRING error” alert, which my server tells me is the below line in bold. Any ideas?

    function mam_previous_post_link_filter ($link='') {
    global $mam_global_stay_in_cat;
    //echo '
    <strong>PREV LINK BEFORE:' . htmlspecialchars($link) . '</strong>
    ';

    Sorry. I’m new to this. I obviously mean the line I incorrectly highlghted as “strong.”

    Yes, it looks like the process of putting the code into the article messed up a bit. The three lines starting with //echo ' and ending with '; should all be on the same line:

    //echo 'PREV LINK BEFORE:' . htmlspecialchars($link) . '';

    The same is true for all the //echo lines.

    They are all just comments, and I will fix them.

    Thanks for pointing out the problem.

    Thanks, I noticed that afterwards. Sometimes I jump the gun, you know. It looks like the code works fine now – I can uncomment all of those ECHO lines, and they output the data I’d expect.

    So, it looks like I have it working partially. The category page picks up the “stayincat” query string and passes it to the single posts. However, the individual posts do not seem to be picking up and reading the stayincat parameter.

    Is there something I’m missing? That one block of code for single.php looks a bit light.

    Here’s the code I’m using on all three pages:

    index.php (functions as category.php in this case):

    if(is_category('9')) { //ONLY WANT THIS TO WORK IN SELECT CATEGORIES
    	$permalink = add_query_arg('stayincat', get_query_var('cat'), get_permalink());
    } else {
    	$permalink = get_permalink();
    }

    sidebar.php (read by single.php):

    <?php if($mam_global_stay_in_cat) { ?>
    <div class="navigation-box">
    	<div class="nav-prev"><?php next_post_link('%link', '&laquo;', 'true'); ?></div>
    	<div class="nav-index"><a href="<?php bloginfo('home'); ?>/category/portfolio/<?php echo $nav_index_category; ?>/">&laquo;&raquo;</a></div>
    	<div class="nav-next"><?php previous_post_link('%link', '&raquo;', 'true'); ?></div>
    </div>
    <?php } ?>

    functions.php has all of the other code and, as mentioned, seems to work fine without any errors.

    Just a guess, but did you declare $mam_global_stay_in_cat as a global in sidebar.php?

    <?php global $mam_global_stay_in_cat;
    if($mam_global_stay_in_cat) { ?>
    <div class="navigation-box">
    	<div class="nav-prev"><?php next_post_link('%link', '&laquo;', 'true'); ?></div>
    	<div class="nav-index"><a href="<?php bloginfo('home'); ?>/category/portfolio/<?php echo $nav_index_category; ?>/">&laquo;&raquo;</a></div>
    	<div class="nav-next"><?php previous_post_link('%link', '&raquo;', 'true'); ?></div>
    </div>
    <?php } ?>

    Sorry, I don’t remember which site I used the exact code in the article. Here is the code in another single.php that might work:

    <div class="navigation">
            <?php $mam_global_stay_in_cat = ($_GET['stayincat']) ? $_GET['stayincat'] : false;
               if ( $mam_global_stay_in_cat ) { ?>
               <div class="alignleft"><?php previous_post_link('&laquo; %link','%title',true) ?></div>
               <div class="alignright"><?php next_post_link('%link &raquo;','%title',true) ?></div>
             <?php } else { ?>
                <div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
                <div class="alignright"><?php next_post_link('%link &raquo;') ?></div>
             <?php } ?>
    		</div>

    I’ll give that second one a shot. Thanks.

    Please let me know if it works for you.

    I obviously need to rework that article. Thanks for your patience.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘previous_post_link AND next_post_link – browse ONLY in same category’ is closed to new replies.