Recently, one of our users asked us if there was a way to display related pages instead of posts in WordPress. Previously, we showed you how to display related posts in WordPress with or without a plugin. In this article, we will show you how to display related pages in WordPress.
The easiest way to display related pages in WordPress is by using a plugin.
The first thing you need to do is install and activate the YARPP (Yet Another Related Posts Plugin) plugin. After activation, you have to go to Settings »YARPP to configure the plugin.
On the plugin settings page, scroll down to 'Display options for your website' section. Under Show automatically Option, you should check 'pages' and leave posts and media unchecked. That's it, you can save your settings now and YARPP will start displaying the related pages below the page content on your WordPress site.
Please note that YARPP, along with some other WordPress plugins, is blocked by some managed WordPress hosting providers due to its heavy database usage. Another problem you may face is that YARPP cannot search the database for text if your MySQL storage engine is set to InnoDB.
Before we show you how to display related pages without using a plugin, we'd like you to take a look at our article on the difference between posts and pages in WordPress.
The most efficient way to display related posts is to search for tags or categories. But since WordPress pages do not have tags or categories, we will first need to enable categories and tags for WordPress pages. To do so, all you need to do is install and activate the Publish Tags and Categories for Pages plugin.
The plugin works out of the box, so there are no settings to configure. Upon activation, it will simply enable tags and categories for your WordPress pages.
Now you need to edit a couple of pages that you think are related to each other and add tags. For example, if you have a page about your company and another page for company history, you can tag them as well as about us.
After adding tags to a few pages, the next thing to do is add this code to your theme's functions.php file or a site-specific plugin.
función wpb_related_pages () $ orig_post = $ post; global $ post; $ tags = wp_get_post_tags ($ post-> ID); if ($ tags) $ tag_ids = array (); foreach ($ tags como $ individual_tag) $ tag_ids  = $ individual_tag-> term_id; $ args = array ('post_type' => 'page', 'tag__in' => $ tag_ids, 'post__not_in' => array ($ post-> ID), 'posts_per_page' => 5); $ my_query = new WP_Query ($ args); if ($ my_query-> have_posts ()) echo '
This code looks for tags associated with a page and then runs a database query to get pages with similar tags. To display the list of pages, you will need to edit the page template. Most commonly it is
content-page.php proceedings. Just add this line of code where you want related pages to appear.
This will display related pages on any WordPress page. It won't look very pretty at first, so you'll need to add some CSS and styling to match your theme.
Note: Code in functions.php is treated the same as plugins.
We hope this article has helped you display related pages in WordPress. As always, feel free to ask us questions in the comments below. Don't forget to follow us on Twitter or join the discussion on Google.+.