Home » WordPress » How to display content from other page in WordPress using shortcode

How to display content from other page in WordPress using shortcode

Few days ago, while rebuilding WordPress website for a customer, I needed to display Frequently Asked Questions in multiple pages.

Instead of copying FAQ information from one page to another I thought it would be better to create a dedicated FAQ page, and display it’s content anywhere I need using shortcode. This way client won’t need to update multiple pages if he ever needs to update the FAQ – it will be much faster and easier to make changes in a single dedicated page.

Today I’ll show you how you can do the same page – load content from one page or post into another page using a shortcode.

How to create a simple shortcode for displaying page content in another pages in WordPress

There are two easy ways to add custom shortcode in WordPress. First way which will be using is to add a custom shortcode function to your WordPress theme’s functions.php file. The other way could be to place the same code you’ll find below into a dedicated WordPress plugin which you can build in 5 minutes.

In this tutorial we’ll be adding code to theme’s functions.php file. Ideally it should be a child theme (like Genesis child theme or any other child theme). But the code will work in any theme’s functions.php file.

Go to Appearance >> Theme File Editor, and open functions.php file at the right side of the page.

wordpress functions.php file

Scroll to the bottom, and add this code:

function display_my_content($attr) {
    $post   = get_post(118);
    $result =  apply_filters( 'the_content', $post->post_content );
    return $result;
}
add_shortcode('faq', 'display_my_content');

Best WordPress hosting 2024

Replace faq with any shortcode name you want, and replace 118 with a post ID of the page you want to display content from. Just make sure that shortcode name would be a single word, or multiple words joined by _ symbol. No other special symbols. It should look something like that:

wordpress custom shortcode function

Don’t forget to save changes by clicking Update File.

How to display content from other page in WordPress using a shortcode

Sice you already created a custom shortcode a few seconds ago, the only thing what’s left is to start using it! Open any page or post via WordPress admin panel, and add a shortcode block where needed.

how to add shortcode in wordpress

Enter your new created shortcode to the shortcode block:

how to enter shortcode to wordpress

Once you save changes content from specified page will be shown in the place you added the shortcode:

wordpress display content from page in another page

Job done!

Tags:

Fathom analytics - privacy focused cookie-free website analytics

Most popular tutorials


Get our latest WordPress news and special offers from RockSolidWP!

Only useful WordPress and WooCommerce tips and tricks and exclusive offers for our readers once a month. No marketing nonsense.

Looking for reliable yet affordable WordPress hosting?
Hostinger is the way to go!

Get 10% OFF by using code IMAKEITWORK