WordPress.org

Ready to get started?Download WordPress

Forums

Specify category for next/prev_post_link (3 posts)

  1. barmin
    Member
    Posted 7 years ago #

    I need to restrict the categorie for the next/prev_post_link tag, since I've got some posts within two or more categories.

    I'm thinking about doing it with an IF statement, like this:

    <?php if ( in_category(4) ) {
    next_post_link();previous_post_link();
    } elseif ( in_category(2) ) {
    next_post_link();previous_post_link();
    }
    ?>

    But there I've got no control whether the post in categories 3 and 4 prints the previous link only for the category 4 as desired.

    So, is there a way to restrict the next_post_link to a specific category? This is the concept:

    <?php if ( in_category(4) ) {
    next_post_link(-for cat. 4-);previous_post_link(-for cat. 4-);
    } elseif ( in_category(2) ) {
    next_post_link(-for cat. 2-);previous_post_link(-for cat. 2-);
    }
    ?>

  2. moshu
    Member
    Posted 7 years ago #

    What did you find on the Codex page describing the tag?
    http://codex.wordpress.org/Template_Tags/next_post_link

  3. barmin
    Member
    Posted 7 years ago #

    I didn't find anything about restricting it to some category. The in_same_cat parameter doesn't work for me.

    I've got 3 categories: "front page", "news" and "health". The index only displays the posts of the "front page" category. But both posts of "news" or "health" could be also categorized as "front page" (depending on the relevance, but it ain't important).

    Therefore, if I'm reading a frontpage-news post, it could jump to a frontpage-health, because they ARE in the same category. That's why I need it to be restricted to a category by using the IF and the category-forced link tag.

Topic Closed

This topic has been closed to new replies.

About this Topic