0

I am wondering if the_post_navigation() function will work properly outside the loop in single views.

For example:

while ( have_posts() ) : the_post();

  get_template_part( 'template-parts/content', 'single' );

endwhile;

the_post_navigation();
0

Yes, the_post_navigation() works outside of the loop on single post views.

Following the function calls, the_post_navigation() uses get_the_post_navigation() which uses get_previous_post_link() and get_next_post_link() which use get_adjacent_post_link() which finally uses get_post() which defaults to the global $post object.

Here's an excerpt of get_adjacent_post_link():

function get_adjacent_post_link( $format, $link, $in_same_term = false,
    $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {

    if ( $previous && is_attachment() )
        $post = get_post( get_post()->post_parent );
    else
        $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );

    if ( ! $post ) {
        $output = '';
    } else {
        $title = $post->post_title;
    ...
| improve this answer | |
  • 1
    Hey Dave, thank you for the detailed answer! It's helpful. – Taras Dashkevych Dec 1 '16 at 20:32

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.