Skip to main content
Fixed typo in get_the_post_navigation()
Source Link
Dave Romsey
  • 17.9k
  • 11
  • 55
  • 70

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

Following the function calls, the_post_navigation() uses the_post_navigationget_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;
    ...

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

Following the function calls, the_post_navigation() uses 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;
    ...

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;
    ...
Source Link
Dave Romsey
  • 17.9k
  • 11
  • 55
  • 70

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

Following the function calls, the_post_navigation() uses 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;
    ...