WordPress.org

Ready to get started?Download WordPress

Forums

Smarter Navigation
How to link prev/next post in parent category? (49 posts)

  1. chavo
    Member
    Posted 1 year ago #

    Hi. I can't figure out how to get prev/next posts links to work as I wish.

    I have a parent category called "Portfolio" and some child categories (web design, identity design, editoria design). Because of functional needs, when I publish a post, I check only the child category where that post belongs.

    When I access to "portfolio" category, I get all the posts belonging to child categories displayed. That's ok. But, when I click on one of this posts, the prev/next links only link to same child category posts but not prev/next posts in parent category "portfolio". I tried checking both categories: parent category "porfolio" and child category...let's say "web design". Prev/next posts links works fine that way, but... wordpress asign main category to each post based in alphabetical order ("portfolio" is before "web design") so another functions that needs the child category slug to work doesn't work anymore.

    So, what I want to do is:
    1) If I came from "Portfolio" category prev/next posts links points to posts in "portfolio" (although the post slug looks like /portfolio/web-design/)

    2) If I came from a child category, for example, "web-design", prev/next post links point to posts belonging to this child category.

    Is this possible?

    Thanks in advance.

    ps: sorry for my english

    http://wordpress.org/extend/plugins/smarter-navigation/

  2. antonel1
    Member
    Posted 1 year ago #

    I have a similar problem. I used http://codex.wordpress.org/Template_Tags/next_post_link to help me get the prev/next to flip through posts within the same child category. However, I can't get prev/next to flip through posts within a parent. It automatically goes to the child category.

    Chavo, were you able to fix this? If not, can anyone help us? Your help would be greatly appreciated!

  3. chavo
    Member
    Posted 1 year ago #

    Hi. At this point (7 months later) I don't remember really well but I think that I did it. Finally I decided to use another way to show my posts so I didn't need this function anymore.

    First, download and install this plugin:
    Ambrosite Next/Previous Post Link Plus

    Second, you need to know where you are coming from (home / category archive). To get this goal I decided to use sessions.

    1) At functions.php you need to enable sessions to work. Put this code at the beggining of the file:

    /**
     *    init_sessions()
     *
     *    @uses session_id()
     *    @uses session_start()
     */
    function init_sessions() {
        if (!session_id()) {
            session_start();
        }
    }
    add_action('init', 'init_sessions');

    2) Enable "history" (where you are coming from?). Put this code in footer.php before <?php wp_footer(); ?>

    $this_category = get_category($cat) // get category ID;
    if (is_home()) {
    $_SESSION['history'] =  "home";
    } else if (is_category('portfolio')) {
    $_SESSION['history'] =  "portfolio";
    } else if ($this_category->category_parent == 1) { // for "portfolio" child categories
    $_SESSION['history'] =  "portfolio subcategory";
    }

    Now you have sessions working, you need to use conditional tags in single.php.

    if(isset($_SESSION['history'])){ // if "history" session exists
    if ($_SESSION['history'] == "home" || $_SESSION['history'] == "portfolio") { // if you come from home or portfolio, use prev/next links plus code
    previous_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => '« PREV', 'ex_cats' => '9'));
    next_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => 'NEXT »', 'ex_cats' => '9'));
    } else if ($_SESSION['history'] == "subcategoria portfolio") { // if you come from a portfolio subcategory use native wordpress prev / next links
    previous_post_link ('<span class="next">%link</span>','« PREV', TRUE);
    next_post_link ('<span class="prev">%link</span>','NEXT »', TRUE);
    }
    } else { // if "history" doesn't exist use prev/next links plus code (maybe this code is not necessary)
    previous_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => '« PREV', 'ex_cats' => '9'));
    next_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => 'NEXT »', 'ex_cats' => '9'));
    }

    Well, like I said, I don't remember really well if this worked ok (I think I get it but I'm not sure). Let me know if this works for you.

    ps: sorry for my english.

  4. antonel1
    Member
    Posted 1 year ago #

    Thank you for the prompt response!

    I have tried this plugin but it does strange things to my website when I put it to work. It's a complicated plugin. I tried your method but everything went blank. If all else fails then I will play around with your solution some more and see if I can get it to work.

    I was hoping for a simpler solution. I may ask a similar question in the 'How-To and Troubleshooting' section for a non plugin solution, if there is one.

    Thank you for your help. If I have any questions or find a solution I will let you know!

    P.S. Your english is great! :o)

  5. chavo
    Member
    Posted 1 year ago #

    Ok. The blank problem maybe is because of a bad closed code. Check if nothing is missing. Try one step at once to check in wich step you get the blank page. Perhaps I forget to close a conditional statement.

  6. antonel1
    Member
    Posted 1 year ago #

    I got the Ambrosite Next/Previous Post Link Plus plugin to work, however, I can't get it to flip through posts in both parent and in categories.

    Chavo, I tried you method again, but it only flips through posts in parent but not category. Should I be replacing 'home' or 'portfolio' with something else? I'm not sure what to replace in your code. And what do you mean by 'enable history'?

    Close to giving up.

  7. chavo
    Member
    Posted 1 year ago #

    Mmmm... How are you managin your post? When you publish a post you check both parent and child category or only the child category?

    In my example the "portfolio" session history have this name because I want to know if visitor comes from "portfolio" category. In your case, you can replace it by your own category.

    In my case, this parameter 'ex_cats' => '9' excludes the category "news" so I cant navigate through post in any portfolio posts (asigned to a portfolio subcategory).

    In the default linking navigation the parameter TRUE tells navigation that you want to navigate between posts in same category.

    So, to be clear:

    If I came from home or portfolio (I display posts that belongs the portfolio subcategories in home page) > Allow posts navigation between posts in ANY portfolio subcategory.

    If I came from a portfolio subcategory, navigate through posts in same subcategory.
    edit: I notice that when I translated my code I left a piece of spanish code, so in your theme code in footer you have to replace
    } else if ($_SESSION['history'] == "subcategoria portfolio") {
    with
    } else if ($_SESSION['history'] == "portfolio subcategory") {

    By "enable history" I want to mean that you have to declare the session. In my case I call it "history".

    I hope to be clearer.

    ps: once again, sorry for my "tarzan english".

  8. antonel1
    Member
    Posted 1 year ago #

    I seem to have solved the problem. I used the code from WordPress Codex. Then made sure that each post has child and parent categories checked off when managing each post.

    I only had child category checked off which is probably why the pagination wasn't working correctly when entering a post from the parent category.

    Thank you Chavo for pointing this out to me! Simple solution. Not sure why I didn't see that. Sorry for all the trouble. And thank you for ALL your help!

  9. chavo
    Member
    Posted 1 year ago #

    No problem, you're welcome!

  10. antonel1
    Member
    Posted 1 year ago #

    I got excited for nothing. I thought I solved the problem but I just realized I didn't. Now that I have both child and parent categories checked off, when entering the post from the child category, it follows the parent category order instead.

    So I'm going to give your solution another try. When I insert the code for footer in footer.php, it does something strange with my wordpress. Could I be missing any brackets? Like [] or () or ;

  11. chavo
    Member
    Posted 1 year ago #

    Tell me what's the problem.

  12. antonel1
    Member
    Posted 1 year ago #

    The wordpress dashboard/bar at the top of the window disappears. The one we usually see when we are logged into our wordpress account.

  13. chavo
    Member
    Posted 1 year ago #

    I always hide the bar (in frontpage) via functions.php but that's not the case.

    Does your code in footer.php looks similar to this?

    <?php
    $this_category = get_category($cat);
    if (is_home() || is_front_page()) {
    $_SESSION['history'] =  "home";
    } else if (is_category('portfolio')) {
    $_SESSION['history'] =  "portfolio";
    } else if ($this_category->category_parent == 1) {
    $_SESSION['history'] =  "portfolio subcategory";
    }
    ?>
    <?php wp_footer(); ?>

    Note: you have to change "is_category('portfolio')" with your category slug and $this_category->category_parent == 1 with the ID of that category (in my case "portfolio" category ID = 1).

  14. antonel1
    Member
    Posted 1 year ago #

    I added php tags (below) around the code or the code won't work. Maybe that's the problem. What should I add around the code instead of php?

    <?php $this_category = get_category($cat) // get category ID;
    if (is_home()) {
    $_SESSION['history'] =  "home";
    } else if (is_category('portfolio')) {
    $_SESSION['history'] =  "portfolio";
    } else if ($this_category->category_parent == 1) { // for "portfolio" child categories
    $_SESSION['history'] =  "portfolio subcategory";
    }  ?>
  15. chavo
    Member
    Posted 1 year ago #

    php tags is correct.

    I'm assuming you know about php code. Do you?

  16. antonel1
    Member
    Posted 1 year ago #

    Well my code for footer must be wrong because your code is working fine now.

    Okay, let me add the code for single.php and see what happens. I'll be a few minutes.

    I'm replacing the word 'portfolio' with my parent name which is 'latest' correct?

  17. chavo
    Member
    Posted 1 year ago #

    Yes. But, just to be clear:

    In this line
    } else if (is_category('portfolio')) {
    the word "portfolio" refers to category slug/name.

    In this line
    $_SESSION['history'] = "portfolio";
    the word "portfolio" refers to the variable name assigned to history session.

  18. antonel1
    Member
    Posted 1 year ago #

    I must be missing something.

    This is the way my categories are set up.

    Latest (parent)
    illustration (child)
    photography (child)
    painting (child)

    I'm not using the index page to display posts. Just category pages like listed above. So latest, illustration, photography, and painting are all category pages.

    Hmmm..

    Footer:

    <?php
    $this_category = get_category($cat);
    if (is_home() || is_front_page()) {
    $_SESSION['history'] =  "latest";
    } else if (is_category('latest')) {
    $_SESSION['history'] =  "latest";
    } else if ($this_category->category_parent == 1) {
    $_SESSION['history'] =  "latest subcategory";
    }
    ?>

    Single:

    <?php
    				if(isset($_SESSION['history'])){ // if "history" session exists
    if ($_SESSION['history'] == "home" || $_SESSION['history'] == "latest") { // if you come from home or portfolio, use prev/next links plus code
    previous_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => '« PREV', 'ex_cats' => '9'));
    next_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => 'NEXT »', 'ex_cats' => '9'));
    } else if ($_SESSION['history'] == "latest subcategory") { // if you come from a portfolio subcategory use native wordpress prev / next links
    previous_post_link ('<span class="next">%link</span>','« PREV', TRUE);
    next_post_link ('<span class="prev">%link</span>','NEXT »', TRUE);
    }
    } else { // if "history" doesn't exist use prev/next links plus code (maybe this code is not necessary)
    previous_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => '« PREV', 'ex_cats' => '9'));
    next_post_link_plus(array('format' => '<span class="prev">%link</span>', 'link' => 'NEXT »', 'ex_cats' => '9'));
    }
    ?>
  19. chavo
    Member
    Posted 1 year ago #

    Ok. Assuming that "latest" category ID is = 1 and that you ONLY have the mentioned categories, please try with the code as follows.

    In footer.php:

    <?php
    $this_category = get_category($cat);
    if (is_category('latest')) {
    $_SESSION['history'] =  "visitorComesFromLatest";
    }
    ?>
    <?php wp_footer(); ?>

    In single.php:

    <?php
    if(isset($_SESSION['history'])){
    if ($_SESSION['history'] == "visitorComesFromLatest") {
    previous_post_link ('<span class="prev">%link</span>','PREV', false);
    next_post_link ('<span class="next">%link</span>','NEXT', false);
    }
    } else {
    previous_post_link ('<span class="prev">%link</span>','PREV', true);
    next_post_link ('<span class="next">%link</span>','NEXT', true);
    }
    ?>
  20. antonel1
    Member
    Posted 1 year ago #

    If it makes a difference, I have one more child category: design :o)

  21. chavo
    Member
    Posted 1 year ago #

    Ok, not a problem. If you have another parent categories, you will need to use other code in single.php.

    Does it works for you?

  22. antonel1
    Member
    Posted 1 year ago #

    I'm so sorry for wasting your time. No, it's not working.

  23. chavo
    Member
    Posted 1 year ago #

    Mmmmm...

    Well, let's try if session is working. Clear the code you add in single.php and try this:

    if(isset($_SESSION['history'])){
    echo "<p>Visitor comes from ".$_SESSION['history']."</p>";
    }

    Tell if you get "Visitor comes from visitorComesFromLatest".

  24. antonel1
    Member
    Posted 1 year ago #

    Yes, I get that.

  25. chavo
    Member
    Posted 1 year ago #

    Perfect! Session is working.

    Let's try again step by step. Tell me if this works:

    if(isset($_SESSION['history'])){
    if ($_SESSION['history'] == "visitorComesFromLatest") {
    previous_post_link ('%link','PREV', false);
    next_post_link ('%link','NEXT', false);
    }
    }
  26. antonel1
    Member
    Posted 1 year ago #

    No, it flips back to the parent order. Same thing.

  27. chavo
    Member
    Posted 1 year ago #

    Yes, I know, is working as expected!

    Maybe I'm not being clear about what this code does. The code checks if you come from parent category "Latest". If it finds session = visitorComesFromLatest, you will navigate between posts in parent category order.

    But what I you select a child category? You will want to navigate ONLY between posts in same category and not parent order. Right?

    Well, to get this working, you will need to put the code as follows:

    if(isset($_SESSION['history'])){
    if ($_SESSION['history'] == "visitorComesFromLatest") {
    previous_post_link ('%link','PREV', false);
    next_post_link ('%link','NEXT', false);
    }
    } else {
    previous_post_link ('%link','PREV', true);
    next_post_link ('%link','NEXT', true);
    }

    Try and tell if I'm right.

  28. antonel1
    Member
    Posted 1 year ago #

    Yup, what you explained is exactly what I want.

    Unfortunately, I tried your code again, and it still doesn't work.

    When I click on a post from the latest category page, it follows latest order.

    However, when I click the very same post from a category (ie: illustration) instead of following the illustration order, it follows latest order and ignores the illustration order.

    I'm clicking the category links from a sidebar, does this change things?

    My Latest category ID is 96 not 1. Where do I put this?

    I'm so sorry. I think I wasted your and my time with this. I appreciate your help. I don't see this working.

  29. chavo
    Member
    Posted 1 year ago #

    Don't get frustrated! We will get it working (it works for me in the past!).

    Before changing code in footer.php, try this way in single.php:

    if ($_SESSION['history'] == "visitorComesFromLatest") {
    previous_post_link ('%link','PREV', false);
    next_post_link ('%link','NEXT', false);
    } else {
    previous_post_link ('%link','PREV', true);
    next_post_link ('%link','NEXT', true);
    }
  30. antonel1
    Member
    Posted 1 year ago #

    You're more determined than I am!

    No, it doesn't work.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic