Many themes include a navigation underneath a post where you can move on to the next or previous post. The disadvantage is often that the links lead to recent posts regardless of the category they are in.

If you have created a good structure among your posts, you are more likely to lead the visitor to the next or previous post in the same category. .
To do this, you need to add some code.
Link to next post
You can put the code in your functions.php in your theme if you have a child theme. Otherwise, you can create an extension that contains code specific to your website, a site plugin.
add_filter( 'next_post_link', 'my_post_link', 10, 5 );
add_filter( 'previous_post_link', 'my_post_link', 10, 5 );
function my_post_link( $output, $format, $link, $post, $adjacent )
{
$previous = 'previous' === $adjacent;
if ( ! ( $previous && is_attachment() ) ) {
$post = get_adjacent_post( true, '', $previous, 'category' );
}
if ( ! $post ) {
$output = '';
} else {
$title = $post->post_title;
if ( empty( $post->post_title ) ) {
$title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
}
$title = apply_filters( 'the_title', $title, $post->ID );
$date = mysql2date( get_option( 'date_format' ), $post->post_date );
$rel = $previous ? 'prev' : 'next';
$string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">';
$inlink = str_replace( '%title', $title, $link );
$inlink = str_replace( '%date', $date, $inlink );
$inlink = $string . $inlink . '</a>';
$output = str_replace( '%link', $inlink, $format );
}
return $output;
}

Keep track of changes
Leave a Reply