Recently, one of our users asked us if there was a way to show related pages instead of posts in WordPress. Previously, we showed you how to show related posts in WordPress with or without plugin. In this article, we will show you how to show related pages in WordPress.
Showing Related Pages in WordPress Using Plugin
The easier way to display related pages in WordPress is by using a plugin.
First thing you need to do is install and activate the Yet Another Related Posts Plugin (YARPP) plugin. After activation, you need to go to Settings » YARPP to configure the plugin.
On the plugin’s settings page, scroll down to ‘Display Options for your website’ section. Under Automatically Display option, you need to check ‘pages’ and leave posts and media unchecked. That’s all, you can save your settings now and YARPP will start displaying related pages below 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 issue that you may face is that YARPP cannot search database for text if your MySQL storage engine is set to InnoDB.
Showing Related Pages in WordPress Without Plugin
Before we show you how to display related pages without using a plugin, we would like you to take a look at our article on the difference between Posts and Pages in WordPress.
Most efficient way to display related posts is by looking 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 that, all you need to do is install and activate, the Post Tags and Categories for Pages plugin.
The plugin works out of the box, so there are not settings for you to configure. Upon activation, it will simply enable tags and categories for your WordPress pages.
Now you need to edit a couple of pages 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’s history, you can tag them both as about us.
After you have added tags to a few pages, the next thing you need to do is to add this code in your theme’s functions.php file or a site-specific plugin.
function wpb_related_pages() { $orig_post = $post; global $post; $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $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 '<div id="relatedpages"><h3>Related Pages</h3><ul>'; while( $my_query->have_posts() ) { $my_query->the_post(); ?> <li><div class="relatedthumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumb'); ?></a></div> <div class="relatedcontent"> <h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3> <?php the_time('M j, Y') ?> </div> </li> <? } echo '</ul></div>'; } else { echo "No Related Pages Found:"; } } $post = $orig_post; wp_reset_query(); }
This code looks for tags associated with a page and then runs a database query to fetch pages with the similar tags. To display the list of pages, you would need to edit your page template. Most commonly it is page.php
or content-page.php
file. Simply add this line of code where you want related pages to appear.
<?php wpb_related_pages(); ?>
This will display related pages on any WordPress page. It will not look very pretty at first, so you will need to add some CSS and style it to match your theme.
Note: code in functions.php are treated the same as plugins.
We hope this article helped you display related pages in WordPress. As always, please feel free to ask us questions in the comments below. Don’t forget to follow us on twitter or join the discussion on Google+.
Your code does not work.
You wrote line 26: <?
instead of: <?php
Hi, thanks i just wanna ask something. İ dont wanna use a plugin to add tags or category on pages. İ just have a custom taxonomy for some pages and some post. And i wanna display related pages is by looking for custom taxonomy. İs there a solution ?
hey there. completely new, so i havea question, where should i put this line of code ->
Do i have to put this line in functions.php as well?And if yes, i dont want related pages to show up in ANY SINGLE page…
No you need to put it in the template where you want to display related posts. Like single.php, category.php, etc.
Thank you! Works for me.
My question is what is the code for conditional statement? Something like function_exists ?
See this article.
Would it be possible to get a list of pages sisters in a certain hierarchy of pages parents?
My major question is how do I put in that div that I will need to style in the css
To reiterate Al Harji’s comment, this tutorial was very timely as this was something I was starting on today with my site. However, when I went to install both YARPP and CRP, WP showed “not available” for both. I’m using GoDaddy’s Managed WP with 2014 theme. This is the first time I’ve received this message for any plugin. Checked the WP support forum but no luck. Any ideas?
I was using YARPP for quite a while. The template part is really awesome but so far I didn’t thought about related PAGES. Great idea for an upcoming project. Thanks for the tip!
Syed,
Would like to pitch in my plugin Contextual Related Posts – https://wordpress.org/plugins/contextual-related-posts/
You can use it to find related posts for posts, pages as well as other custom post types. It comes armed with a host of settings and some cool thumbnails style.
Great tutorial as always, this one is so timely – thank you!
Ajay, your plugin looks amazing. I am looking forward to trying it and also impressed with so many positive reviews. I use your Top 10 plugin which I love as well.