Documentation - How To - Previous Post Link

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

Previous Post Link

Being used mainly on single post template, this tag display a link to a previous post relative to current post, from a sort list.

This tag must be used within the query loop.

The default WordPress function used for previous naivigational link is previous_post_link. This is very limited in functionality and there is no possibility to apply a sort list and a specific taxonomy term to which the post is related to.

An improvement for that function is previous_post_type_link

Arguments:

previous_post_type_link($format, $link, $argv);

If $argv is empty the archive order will be used.

Parameters

$format(string) Format string for the link. This is where to control what comes before and after the link. '%link' in string will be replaced with whatever is declared as 'link' (see next parameter). 'Go to %link' will generate “Go to <a href=…” Put HTML tags here to style the final results. Defaults to '&laquo; %link'.

$link(string) Link text to display. Defaults to previous post’s title ('%title').

$argv(array) List of function arguments:
sort_id – the sort id which order will be used
taxonomy – use the specified sort taxonomy
term_id – use the specified sort taxonomy term

Examples

Return previous post link using order from taxonomy ‘Features’ within term_id 10

$args   =   array(
                        'sort_id'   =>  25,
                            'taxonomy'  =>  'features',
                            'term_id'   =>  10
                        );
previous_post_type_link( '%link', __( '<span class="meta-nav">Previous Post</span> %title', 'twentyfourteen' ), $args );

Return the previous link to a post in current category

$post_category  =   get_the_category()['0']->term_id;
$args   =   array(
                        'sort_id'   =>  25,
                            'taxonomy'  =>  'features',
                            'term_id'   =>  $post_category
                        );
previous_post_type_link( '%link', __( '<span class="meta-nav">Previous Post</span> %title', 'twentyfourteen' ), $args );

Usign WPML return previous post link using order from taxonomy ‘Features’ within term_id 10 (default language)

$args   =   array(
                        'sort_id'   =>  25,
                            'taxonomy'  =>  'features',
                            'term_id'   =>  apply_filters( 'wpml_object_id', 10, 'category', TRUE  )
                        );
previous_post_type_link( '%link', __( '<span class="meta-nav">Previous Post</span> %title', 'twentyfourteen' ), $args );