How to Include a Specific Page’s Content in a WordPress Theme without a Plugin

Sometimes you want to be able to include a specific page’s content in some other portion of your WordPress Theme. Since just about the beginning of WordPress’ existence, the Improved Include plugin served this function well. Though it hasn’t been updated in a couple of years, it may even still work, but as we get more advanced with our WordPressery we begin to desire to pull off feats like this without the use of a plugin.

Today, I’m here to show you precisely how to do that; we’ll be creating a function in our Theme’s functions.php file which allows us to define the page’s content directly through a call on any other theme template page.

Step 1. Create the Function

Open up your theme’s functions.php file and paste the following code, likely at the bottom of the page.

function cn_include_content($pid) {
$thepageinquestion = get_post($pid);
$content = $thepageinquestion->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}

Step 2. Use the Function in a Template File

Next, open up the specific template file you want to include the Page’s content on and paste in the function:

<?php cn_include_content(42); ?>

Replace 42 with the ID of the Page you want to include.

Optional Step 3. Create a Shortcode

For extra karate kicks, let’s create a shortcode that we can use to do the same thing, but via the WordPress content editor. Back in our functions.php file, add:

function cn_include_page( $atts, $content = null ) {
extract(shortcode_atts(array( // a few default values
'id' => '')
, $atts));
cn_include_content($id);
}
add_shortcode('includepage', 'cn_include_page');

We can now use the following shortcode on any Page, Post or Custom Post Type:

[includepage id="42"]

Again, where 42 is the ID of the actual Page you want to include.

Up Next: CSS Page Curl Effect without Using Images