WordPress.org

Ready to get started?Download WordPress

Forums

Restrict "previous_post_link" and "next_post_link" to same child category (15 posts)

  1. tshirtfiend
    Member
    Posted 2 years ago #

    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?

  2. timDesain
    Member
    Posted 2 years ago #

    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); ?>

  3. tshirtfiend
    Member
    Posted 2 years ago #

    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?

  4. MatthewEbel
    Member
    Posted 2 years ago #

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

  5. tshirtfiend
    Member
    Posted 2 years ago #

    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.

  6. charliechocolate
    Member
    Posted 2 years ago #

    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.

  7. keesiemeijer
    moderator
    Posted 2 years ago #

    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); ?>
  8. charliechocolate
    Member
    Posted 2 years ago #

    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"

  9. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  10. charliechocolate
    Member
    Posted 2 years ago #

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

  11. keesiemeijer
    moderator
    Posted 2 years ago #

    I think your site is in maintenance mode.

  12. charliechocolate
    Member
    Posted 2 years ago #

    Oops sorry. All open now.

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  14. charliechocolate
    Member
    Posted 2 years ago #

  15. tshirtfiend
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic