1. Home
  2. /
  3. Web Design & Development
  4. /
  5. WordPress
  6. /
  7. How to Implement Next and Previous Article Navigation Link With Title? And, Using Custom Icons

How to Implement Next and Previous Article Navigation Link With Title? And, Using Custom Icons

How to Implement Next and Previous Article Navigation Link?

Sometimes, instead of traditional numeric pagination there might be a need to add some sort of links on left-right pointing arrows to navigate through the next and previous articles in a WordPress blog. In such cases we can use the wordPress functions such as next_post_link and previous_post_link. These functions can be customized by using filters. Here is an example demostrating the use of these functions to not only customize it to navigate between posts but also add an anchor title (having a post title) on the links as well as replacing any default arrows with your own custom left and right arrow. Let’s explore the steps to implement next and previous article navigation link.

In this blog, we’ll explore the steps to implement next and previous article navigation link with title, importance of implement them, and the cons you may face if you implement them.

How to Implement Next and Previous Article Navigation Link With Title?

To implement next and previous article navigation link, you will need to use a combination of HTML, CSS, and JavaScript. Here are the general steps you can follow:

1. Create the HTML structure for your articles, including the next and previous navigation links. You can use anchor tags (<a>) for the links, and give them unique class or ID attributes for styling and scripting purposes.

2. Use CSS to style the navigation links. You can use the class or ID attributes from step 1 to select the links and apply styles such as color, font, and hover effects.

3. Use JavaScript to add functionality to the navigation links. You can use the class or ID attributes from step 1 to select the links, and then use the addEventListener() method to attach click event handlers to them.

4. Within the event handlers, you can use JavaScript to navigate to the next or previous article by manipulating the window.location.href property, or by using the pushState() method if you prefer to use the history API.

5. You will also need to keep track of the current article being viewed, so you can determine which article should be loaded when the next or previous links are clicked. You can use a JavaScript variable to store this information, and update it each time the user clicks a navigation link.

Related: How to Calculate Read Time of an Article or Textual Content?

WordPress Function Call for Next-Previous Link Display

The below script could be placed inside the wordPress theme single.php or content template part file at the start or end of the page where you would like to display the next-previous navigation links.

<div class="prevnxtnav">
<?php previous_post_link('%link'); ?>&nbsp;<?php next_post_link('%link'); ?>
</div>

Here, we are passing ‘%link’ through previous_post_link and next_post_link function calls to ensure we replace the WordPress default left and right arrows.

WordPress Filters to Override the Default Anchors

Add the following script inside the themes functions.php file at any place. Make sure to close it properly and use it within the php start and close tags.

next_post_link filter

In this function override, we are replacing the anchor text with the next post title and adding the post title to the anchor’s title for the next_post_link. Example:

add_filter('next_post_link', function($nxt_post_link) {
  $next_post = get_next_post();
  $post_title = trim($next_post->post_title); 
  $nxt_post_link = str_ireplace($post_title, '<img src="/wp-content/themes/yourthemefilename/images/next.svg" alt="Next Article" />', $nxt_post_link);
  $nxt_post_link = str_ireplace('href=', 'title="'.$post_title.'" href=', $nxt_post_link);  
  return $nxt_post_link;
});

previous_post_link filter

In this filter call override, we are replacing the anchor text with the previous post title and to the anchor’s title for the previous_post_link. Example:

add_filter('previous_post_link', function($prev_post_ink) {
  $previous_post = get_previous_post();
  $post_title = trim($previous_post->post_title);  
  $prev_post_ink = str_ireplace($post_title, '<img src="/wp-content/themes/yourthemefilename/images/previous.svg" alt="Previous Article" />', $prev_post_ink);
  $prev_post_ink = str_ireplace('href=', 'title="'.$post_title.'" href=', $prev_post_ink);
  return $prev_post_ink;
});

Now, clubing all scripts, you will be able to display left-right icon based navigations with anchor post titles.

Read also: How to Use Custom Social Icons in a HubSpot Email Template?

Why Should You Implement Next and Previous Article Navigation Link?

Implementing next and previous article navigation links can provide several benefits for both users and search engines:

1. User experience

Navigation links make it easy for users to move between related articles on your site, which can improve their overall experience and engagement. This is especially important for sites with a lot of content, as it can help users find the information they are looking for more quickly.

2. Search engine optimization (SEO)

Navigation links can help search engines understand the structure and organization of your site, which can improve your site’s visibility and ranking in search results. Navigation links also help search engines to understand the context of your content, which can make it more likely to be relevant to the user’s search query.

3. Increase page views

Navigation links can increase the number of pages viewed per user session. This is helpful for both the user and the website because it allows the user to discover more content and the website to increase its ad revenue.

4. Improve internal linking

Navigation links can improve the internal linking structure of your site, which can help search engines understand the importance of different pages and help users find the content they are looking for more easily.

5. Accessibility

Navigation links can help users with accessibility needs, such as those who rely on screen readers, to move through your content more easily.

So, implementing next and previous article navigation links can help improve the user experience, increase the number of page views, and improve search engine optimization, which can ultimately drive more traffic and engagement to your site.

Related: How to Implement Infinite Pagination in WordPress?

Cons to Implement Next and Previous Article Navigation Link

While implementing next and previous article navigation links can provide several benefits, there are also some potential drawbacks to consider:

1. Clutter

Navigation links can add visual clutter to your site, especially if they are not well-designed or if they are used excessively. This can make it more difficult for users to find the content they are looking for, and can also detract from the overall aesthetic of your site.

2. Increased page load time

Navigation links can increase the number of requests made to the server, which can slow down the page load time. This can be especially problematic for users with slow internet connections or for sites with a lot of traffic.

3. Reduced user control

Navigation links can take users away from the content they are currently viewing, which can be frustrating if they are not interested in the linked content.

4. Reduced user engagement

Navigation links can encourage users to move on to the next article quickly, which can reduce the amount of time they spend on each article as well as ultimately reduce user engagement.

5. Limited scalability

If a website has a large number of articles, it will become difficult to maintain next as well as previous links manually. In this case, it is better to use pagination.

6. Not all users find them useful:

Some users prefer to find their way around a website by using the menu or search bar, and may not find navigation links particularly helpful.

Moreover, it is important to balance these potential drawbacks against the benefits of implementing next and previous article navigation links, and to consider the specific needs and goals of your site and your users when deciding whether or not to use them.

Also read: How to set up Goal Conversion tracking on Google Analytics (GA4) with Google Tag Manager

Conclusion

In conclusion, implementing next and previous article navigation links can be a great way to improve the user experience, increase engagement, and boost search engine optimization for your website. By providing users with easy access to related content, you can help them find the information they need more quickly and easily, and by providing search engines with a clear understanding of the structure and organization of your site, you can improve your visibility and ranking in search results.

However, it’s important to be mindful of the potential drawbacks of navigation links and to consider the specific needs and goals of your site and your users when deciding whether or not to use them. Additionally, it’s also important to include the title of the next and previous post to make it more user-friendly and informative.

Furthermore, with the use of WordPress filters, it is possible to add custom class and ID attributes and even change the entire link output. With these tips in mind, you can successfully implement next and previous article navigation links on your website and take your user experience to the next level.

Reviews & Ratings Get your stoe online with Shopify in 60 minutes Shop Now