• 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

    https://www.ads-software.com/extend/plugins/smarter-navigation/

Viewing 15 replies - 31 through 45 (of 48 total)
  • I’m from Canada. And you? Eastern Standard Time Zone. :o)

    I get the following when I enter post from Latest:
    Visitor comes from visitorComesFromLatest

    I get the following when I enter post from Illustration:
    Visitor comes from visitorComesFromLatestSubcategory

    I’m guessing that’s a good thing, right? Not sure why your code is not working, it seems right to me.

    Thread Starter chavo

    (@chavo)

    I’m from Argentina (near to Buenos Aires).

    Ok, I just wanted to check that sessions works even for subcategory.

    Please, in single.php, just to try if link navigation between posts in same category works, put this:

    <?php prev_post_link('%link', 'Prev post in same category', TRUE); ?>
    <?php next_post_link('%link', 'Next post in same category', TRUE); ?>

    Wow, that’s pretty neat!

    Yes, those links work. When I enter same post from Latest and Illustration, it follows Latest order.

    Again, I have post checked under Latest category and Illustration category. When there is more than one category checked off, default chooses any of those categories and follows it. Which in this case would be Latest.

    Thread Starter chavo

    (@chavo)

    Ha! Well, if you check both parent and child, it will navigate based on parent (that’s the reason why wasn’t working).

    Just check child category and try again with this in single.php

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

    If I access post from Latest, the prev/next links don’t show anymore.

    If I access post from Category, the prev/next links show but follow the Latest order.

    Chavo, I need to step away from the computer for 1 or 2 hours. I’m sure you are busy with other things too. We can either continue this a bit later or tomorrow. Let me know what you prefer. I appreciate all your help.

    I’m back. :o)

    Thread Starter chavo

    (@chavo)

    Ok, me too. Try to clear your cache. Do you uncheck parent category and left only the child category selected?

    Great! Cleared cache. I actually did try to uncheck parent for some of the posts to see what would happen. If Illustration is checked for a post but not Latest, then when I access post from category page, the prev/next links remain in the category order, in this case, Illustration. But if Illustration and Latest is checked, then it reverts back to the Latest order.

    Thread Starter chavo

    (@chavo)

    Let’s try with this (make a proof checking both). Remember to clear your cache.

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

    ps: I don’t know why I can’t get I working. I get it in the past. Maybe my memory is not working ok, hahaha.

    Cleared cache.

    There was something missing in the code, so I added ” to “visitorComesFromLatestSubcategory” to get it to work.

    Same problem. When I access the same post from Latest (parent) and Category (child), it follows the parent order, not child.

    Determined, I went ahead. I unchecked ‘Latest’ for some of the posts to see what would happen. Now it seems to be doing what we want it to do. When I access the post from the Latest category, it follows the Latest order. When I access the same post from the Illustration category, it follows the Illustration order. So as long as the post has one category checked off, it works, it can’t have both.

    Does this solve the problem?

    Thread Starter chavo

    (@chavo)

    I think so!

    It works as you wish from begining?

    ps: yes, my mistake. I forget opening ”

    So I unchecked parent category to all the posts that were checked with parent category (Latest) and just left the child categories checked (ie. Illustration). I cleared cache and tested it out again. And again. It seems to be working fine.

    Chavo, thanks for all YOUR help. I would have given up a long time ago if it weren’t for your willpower! If you ever need any help with WordPress, please contact me, I will try my best to help. Just reply to any messages I’ve got open via my Profile. Again, thank you!!

    Thread Starter chavo

    (@chavo)

    You are welcome!

    So to conclude (in case anyone wants to do something similar) this is how we got it to work:

    Download this plugin: Ambrosite Next/Previous Post Link Plus

    Insert the following:

    To enable sessions in WordPress, insert this code in your functions.php page:

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

    Insert this code in your footer.php page, right before wp_footer code:

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

    Then insert this code in your single.php page, where you want next and previous links to go:

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

    Notes:
    – In the codes above, replace the word latest with your parent category name
    – Replace the number ’96’ with the ID of your parent category.
    – This was my structure:
    Latest (parent)
    Illustration (child)
    Painting (child)
    Photography (child)
    – Your posts can only have one category checked off. A post cannot share both parent and child categories, or this code won’t work properly.

    Hope that helps! Again, thank you Chavo!

Viewing 15 replies - 31 through 45 (of 48 total)
  • The topic ‘[Plugin: Smarter Navigation] How to link prev/next post in parent category?’ is closed to new replies.