4

I've been pouring over this problem that seems really minor, but I can't seem to solve it. I'm trying to make next/previous links that only link to the category that the post is in. This is for a custom post type called portfolio. This is the code I currently have on the content-portfolio page:

<?php next_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '←', 'Next post link', 'morphology' ) . '</span> %title'); ?>
<?php previous_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>'); ?>

I've seen some variations of the following, but I can't seem to edit it to get it to cooperate:

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

Any advice as to how to make this work or what I'm doing wrong?


Update: I still haven't gotten a good solution to this, and nothing I've tried from the other top Google hits seem to be working. As soon as I say that in_same_term = true, the link disappears entirely. Based on the answer below, this is the code I'm using:

<?php next_post_link( 
'<div class="nav-previous">%link</div>', 
'<span class="meta-nav">' . _x( '←', 'Next post link', 'morphology' ) . '</span> %title', true, array(), 'portfolio'); ?>
3

previous_post_link takes 5 params, but you use only 2 of them.

Let's take a look at other 3:

  • in_same_term (boolean) (optional) Indicates whether previous post must be within the same taxonomy term as the current post. If set to 'true', only posts from the current taxonomy term will be displayed. If the post is in both the parent and subcategory, or more than one term, the previous post link will lead to the previous post in any of those terms. true false Default: false
  • excluded_terms (string/array) (optional) Array or a comma-separated list of numeric terms IDs from which the next post should not be listed. For example array(1, 5) or '1,5'. This argument used to accept a list of IDs separated by 'and', this was deprecated in WordPress 3.3 Default: None
  • taxonomy (string) (Optional) Taxonomy, if $in_same_term is true. Added in WordPress 3.8. Default: 'category'

So if you want to navigate through posts from the same category, third param should be set to true.

<?php
    previous_post_link(
        '<div class="nav-next">%link</div>',
        '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>',
         true
    );
?>

And if your portfolio uses custom taxonomy, you should point that out in last param:

<?php
    previous_post_link(
        '<div class="nav-next">%link</div>',
        '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>',
         true,
         array(),
         'my_custom_taxonomy'
    );
?>

It works the same with next_post_link.

  • First, I really appreciate you laying things out for me. That was really helpful in understanding how this works. I'm still learning PHP and sometimes I run into these things I just don't quite get. Unfortunately, the suggestion didn't work. I'm not using custom taxonomy, just categories within the custom post type. It looks like array() is param #4 without any input, though shouldn't #4 and #5 be optional? When I end after true, it breaks the site. Otherwise though the links just don't show. – Echo Martin Jul 31 '18 at 3:01
  • So this is the code that I'm using. When #3 is marked as true, nothing appears. When it's marked as false the links show up, but obviously not restricted to the current category: <?php previous_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '→', 'Previous post link', 'morphology' ) . '</span>', false, array(), 'category' ); ?> – Echo Martin Jul 31 '18 at 3:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.