• I would like to be able to navigate between posts in the same child category. I know with previous_post_link and next_post_link you can restrict navigation to the same parent category, but I can’t seem to find a way to restrict it to just the child category with the parameters available. Am I missing something?

Viewing 14 replies - 1 through 14 (of 14 total)
  • have you read this page?

    http://codex.wordpress.org/Function_Reference/next_post_link

    Text As Link, Without Post Title, Within Same Category
    Displays custom text as link to the next post within the same category as the current post. Post title is not included here. “Next post in category” is the custom text, which can be changed to fit your requirements.

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

    Thread Starter tshirtfiend

    (@tshirtfiend)

    Yes, I did read that page. I have the parameters set up as in the snippet you posted. The issue is that the posts in question all share a parent category, but have different child categories.

    If I set the “in category” parameter to “TRUE”, it goes through the posts in the same parent category, but doesn’t restrict it to the child.

    Any thoughts?

    MatthewEbel

    (@matthewebel)

    Yeah, I’m having this problem too… I just want to be able to navigate within a specific category, not its parent.

    Thread Starter tshirtfiend

    (@tshirtfiend)

    Let me know if you figure it out. I still haven’t found a solution…and it’s a bit of a sore spot on my site.

    Bump.

    Did anybody find a solution to this? This issue seems to be all over the place but no solution for it so far. There must be a way around it. I am in desperate need for it too.

    Moderator keesiemeijer

    (@keesiemeijer)

    From the codex:

    If the post is in both the parent and subcategory, or more than one category, the next post link will lead to the next post in any of those categories.

    If these post are only assigned to a single child category you can do something like this:

    <?php
    $exclude_ancestors = array();
    $cat = get_the_category();
    // check if the first post category is a child category
    if($cat[0]->parent > 0) {
    	$exclude_ancestors = get_ancestors( $cat[0]->term_id, 'category' );
    }
    ?>
    <?php next_post_link('%link', 'Next post in category', TRUE, $exclude_ancestors); ?>

    Thank you. Your code works as you said. But since I have some posts with multiple categories, once it reaches one of these it loses track of the original category it came from. I would need it to retain the original category from the first post.

    I am also running into a weird behavior by the menu item these categories belong to. Instead of activating the corresponding main level menu item, it activates the one corresponding to the blog page. There is no relation at all between these two menu items though.

    http://bruto.se/wp/proyectos/

    Note how when one of the category items under the “projects” menu is clicked it deactivates “projects” and activates “news”

    Moderator keesiemeijer

    (@keesiemeijer)

    But since I have some posts with multiple categories, once it reaches one of these it loses track of the original category it came from.

    That is the difficulty If a post is assigned to multiple categories. The code cannot guess what category you want or what the first post was.

    OK, thanks. Any chance you could put me in the right way to get to solve the issue regarding the menu behavior?

    Moderator keesiemeijer

    (@keesiemeijer)

    I think your site is in maintenance mode.

    Oops sorry. All open now.

    Moderator keesiemeijer

    (@keesiemeijer)

    Can you paste and submit the full code of your navigation code into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

    Thread Starter tshirtfiend

    (@tshirtfiend)

    @keeslemeijer What you pasted from the Codex is what we had already discussed about. I’m having the same issue as charliechocolate in that I want to keep navigation inside the same category, even if the post has multiple categories.

    Any ideas?

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Restrict "previous_post_link" and "next_post_link" to same child category’ is closed to new replies.