• 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/

Viewing 15 replies - 1 through 15 (of 48 total)
  • antonel1

    (@antonel1)

    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!

    Thread Starter chavo

    (@chavo)

    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.

    antonel1

    (@antonel1)

    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)

    Thread Starter chavo

    (@chavo)

    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.

    antonel1

    (@antonel1)

    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.

    Thread Starter chavo

    (@chavo)

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

    antonel1

    (@antonel1)

    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!

    Thread Starter chavo

    (@chavo)

    No problem, you’re welcome!

    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 ;

    Thread Starter chavo

    (@chavo)

    Tell me what’s the problem.

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

    Thread Starter chavo

    (@chavo)

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

    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";
    }  ?>
    Thread Starter chavo

    (@chavo)

    php tags is correct.

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

    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?

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