0

In the twentyfifteen theme on the single.php template they are using the fairly new function 'the_post_navigation'. It seems to work for Custom Post Types (CPTs) but I cannot figure out how it does this.

Ultimately, what I would like to do is set the '$taxonomy' parameter in the below function to the current post. However, I cannot figure out how to get the taxonomy of a Post on a single post page.

// How to set $taxonomy to current post taxonomy
get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy = 'category' );
1
  • You don't have to tweak get_adjacent_post_link, just set argument with your taxonomy under the_post_navigation
    – Jevuska
    Commented Apr 5, 2016 at 4:26

1 Answer 1

2

'the_post_navigation'. It seems to work for Custom Post Types (CPTs) but I cannot figure out how it does this.

the_post_navigation() uses get_the_post_navigation() which uses get_previous_post_link() and get_next_post_link() which both uses get_adjacent_post_link() which uses get_adjacent_post() to get the next and previous post from the same post type.

If we look at the source code for get_adjacent_post(), we will notice that the $post_type property from the current post object ($post->post_type) is used as post type for the SQL query. This is how the links "know" how to get posts from the same post type. Here is the relevant line responsible for this

$where = apply_filters( 
    "get_{$adjacent}_post_where", 
    $wpdb->prepare( 
        "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type 
    ), 
    $in_same_term, 
    $excluded_terms, 
    $taxonomy, 
    $post 
);

I cannot figure out how to get the taxonomy of a Post on a single post page.

The taxonomy would be the the one which the terms are assigned to which is assigned to the posts. This is the custom taxonomy registered by you, or it might be the build in taxonomy category or post_tag. To learn what taxonomies and terms are, please refer to this post. The taxonomy name you have to hardcode, so you will need to know which taxonomy's terms you need to target and loop through

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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