Customize the_post_navigation

Below is an example of the_post_navigation customized to include an image and custom text for previous and next.

Add this snippet to your functions file, link to your images, and then call it where you would like the_post_navigation to appear.

function themename_the_post_navigation( $args = array() ) {
    $args = wp_parse_args( $args, array(
        'prev_text'          => '<img src="'.get_bloginfo('stylesheet_directory').'/img/blue-arrow-prev.png" class="arrow" /> Older',
        'next_text'          => 'Newer  <img src="'.get_bloginfo('stylesheet_directory').'/img/blue-arrow.png" class="arrow" />',
        'in_same_term'       => false,
        'excluded_terms'     => '',
        'taxonomy'           => 'category',
        'screen_reader_text' => __( 'Post navigation' ),
    ) );
 
    $navigation = '';
 
    $previous = get_previous_post_link(
        '<div class="nav-previous">%link</div>',
        $args['prev_text'],
        $args['in_same_term'],
        $args['excluded_terms'],
        $args['taxonomy']
    );
 
    $next = get_next_post_link(
        '<div class="nav-next">%link</div>',
        $args['next_text'],
        $args['in_same_term'],
        $args['excluded_terms'],
        $args['taxonomy']
    );
 
    // Only add markup if there's somewhere to navigate to.
    if ( $previous || $next ) {
        $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'] );
    }
 
    return $navigation;
}

Place in theme:

echo themename_the_post_navigation();

Example appearance:

custom previous and next with arrows

Sourced here.

5 thoughts on “Customize the_post_navigation

  1. HI, can i exclude some pages from pagination. I add this code to a list of child pages? and dont want to show links to their parent pages. Thanks in a advance)

    1. Hey, this snippet is geared more towards Post pagination, but you can still control what is seen in the paging by updating ‘taxonomy’ => ‘category’ to a specific taxonomy in order to stay within a certain category.

      I think this is more what you’re looking for. It will allow you to choose the Post Type (to Pages), and you can add post_parent to get_posts() to stay within the child items.

      Let me know if that helps!

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.