WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Show Related Pages in WordPress

How to Show Related Pages in WordPress

Last updated on March 14th, 2014 by Editorial Staff
46 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
How to Show Related Pages in WordPress

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.

Configue YARPP to only show related pages

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+.

46 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

13 Comments

Leave a Reply
  1. Riccardo says:
    Oct 31, 2017 at 6:55 am

    Your code does not work.
    You wrote line 26: <?
    instead of: <?php

    Reply
  2. Alperen says:
    Jan 23, 2017 at 3:26 pm

    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 ?

    Reply
  3. panos gew says:
    Apr 19, 2016 at 9:38 pm

    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…

    Reply
    • WPBeginner Support says:
      Apr 20, 2016 at 1:05 pm

      No you need to put it in the template where you want to display related posts. Like single.php, category.php, etc.

      Reply
  4. George says:
    Nov 13, 2015 at 2:15 am

    Thank you! Works for me. :)

    Reply
  5. WPBeginner Fan says:
    Jan 25, 2015 at 7:59 pm

    My question is what is the code for conditional statement? Something like function_exists ?

    Reply
    • WPBeginner Staff says:
      Jan 25, 2015 at 9:18 pm

      See this article.

      Reply
  6. Marcelo Luiz says:
    Aug 16, 2014 at 5:03 pm

    Would it be possible to get a list of pages sisters in a certain hierarchy of pages parents?

    Reply
  7. Ayebal John Bob says:
    May 27, 2014 at 4:30 am

    My major question is how do I put in that div that I will need to style in the css

    Reply
  8. Rich says:
    Mar 30, 2014 at 9:51 am

    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?

    Reply
  9. Gerald says:
    Mar 24, 2014 at 5:57 pm

    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!

    Reply
  10. Ajay says:
    Mar 14, 2014 at 10:24 am

    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.

    Reply
    • Al Harji says:
      Mar 16, 2014 at 10:09 am

      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.

      Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
OptinMonster
OptinMonster
Convert website visitors into email subscribers. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
MainWP
MainWP Coupon
Get 15% OFF on MainWP WordPress multisite manager plugin.
Sprout Invoices
Sprout Invoices Coupon
Get 50% OFF on Sprout Invoices WordPress invoicing plugin.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.