WordPress.org

Ready to get started?Download WordPress

Forums

previous_post_link AND next_post_link - browse ONLY in same category (19 posts)

  1. mrsamdk
    Member
    Posted 3 years ago #

    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>
  2. vtxyzzy
    Member
    Posted 3 years ago #

    This Stay In Category article might have an answer for you.

  3. jasonlcook
    Member
    Posted 3 years ago #

    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

  4. vtxyzzy
    Member
    Posted 3 years ago #

    @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.

  5. jasonlcook
    Member
    Posted 3 years ago #

    great tip thank you very much.

  6. falariem
    Member
    Posted 3 years ago #

    @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.

  7. vtxyzzy
    Member
    Posted 3 years ago #

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

  8. falariem
    Member
    Posted 3 years ago #

    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>
    ';
  9. falariem
    Member
    Posted 3 years ago #

    Sorry. I'm new to this. I obviously mean the line I incorrectly highlghted as "strong."

  10. vtxyzzy
    Member
    Posted 3 years ago #

    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.

  11. falariem
    Member
    Posted 3 years ago #

    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.

  12. falariem
    Member
    Posted 3 years ago #

    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.

  13. vtxyzzy
    Member
    Posted 3 years ago #

    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 } ?>
  14. vtxyzzy
    Member
    Posted 3 years ago #

    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>
  15. falariem
    Member
    Posted 3 years ago #

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

  16. vtxyzzy
    Member
    Posted 3 years ago #

    Please let me know if it works for you.

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

  17. falariem
    Member
    Posted 3 years ago #

    DUDE (you are a dude, right?)!!! You're a genius! That last snippet you sent works perfectly, without any tweaking.

    So, if you're going to rework the article, here's my feedback:
    1. the code you say to put in functions.php works just fine, with those comment segments fixed.

    2. The new code for single.php (which you posted above) also works great without any changes.

    3. The code segment in archive/category.php has one hiccup, I think. When I use the following line:
    $permalink = add_query_arg('stayincat', get_query_var('cat'), $permalink);

    It doesn't work for some reason. However, when I switch that second $permalink to get_permalink(), it functions properly.

    Thanks again! Hope that hopes. :D

  18. falariem
    Member
    Posted 3 years ago #

    *helps.

    Gods, I'm tired. Thanks again. You saved me several hours of frustration.

  19. vtxyzzy
    Member
    Posted 3 years ago #

    Many thanks for debugging my code!! I really appreciate it.

    I had set $permalink earlier with get_permalink(), but neglected to include that line. Fortunately, the correct code is in the pastebin link.

    Sincere apologies.

Topic Closed

This topic has been closed to new replies.

About this Topic