Include Content From One Page Within Another Page – Genesis Framework

NOTE: The following information is valid for Genesis 1.9.x. Once Genesis 2.0 is released, change the text “genesis_after_post_content” to “genesis_entry_content” in the Include Page template code below. This site in running Genesis 2.0 Beta.

Following is a WordPress page template I created with input from other Genesis Framework professionals. It allows you to include content from one page into another page. The included content is displayed below any original content. This is very useful when you have content that needs to be shared amongst numerous pages. Edit the shared content on one page and all pages including that content will be updated accordingly.

Create the Page Template

Create a file named “include-page.php” and paste in the code below.

<?php
/*  Template Name: Include Page */ 

add_action('genesis_after_post_content', 'include_page');

function include_page() {
	
        $page_id = genesis_get_custom_field('includepage');
        $page_data = get_page( $page_id );
        $title = $page_data->post_title;
        $content = apply_filters('the_content', $page_data->post_content);
		echo '<h1 class="entry-title">'.$title.'</h1>';
		echo '<div class="entry-content">'.$content.'</div>';
        
}

genesis();

Place this new page template file in the root of the child theme folder.

Add New Pages

Add a new page named “Shared Page“. Enter the shared content into this page. Make note of the “Page ID” WordPress assigns to this page.

Add a new page named “Combined Page”. On this page select the “Include Page” template in the pull down menu within the Page Attributes box. Create a custom field named “includepage” and enter the Page ID of the page you wish to include into the value field. Click the Update button.

The “Combined Page” should now display any content you have entered plus include content from the “Shared Page” below it.

The page names I used are for demonstration purposes. You can use whatever page names you prefer. You may choose to assign a <h2> tag and a different class to the title in the page template above since it may not be the best practice to have two page titles with a <h1> tag in the same page.

Include Page Content Without Title

If you prefer not to display the title of the included page, the page template would look like this:

<?php
/*  Template Name: Include Page */ 

add_action('genesis_after_post_content', 'include_page');

function include_page() {
	
        $page_id = genesis_get_custom_field('includepage');
        $page_data = get_page( $page_id );
        $content = apply_filters('the_content', $page_data->post_content);
		echo '<div class="entry-content">'.$content.'</div>';
        
}

genesis();