Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: include content of one page in another

Tags:

php

wordpress

How do I include the page content of one or more page in another page?

ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX

is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??

like image 987
Zebra Avatar asked Nov 03 '10 19:11

Zebra


People also ask

How do I get the contents of another page in WordPress?

First thing you need to do is install and activate the plugin. For more details, see our step by step guide on how to install a WordPress plugin. Upon activation, you need to open up the post or page where you want to embed page content. After that, click the 'Plus' add block icon and search for 'Insert Pages'.

How do I embed a page in WordPress?

Log in to your WordPress admin panel. In the left column navigation mouse over the “Plugins” link and click the “Add New” link. In the “Search plugins…” box, enter “Insert Pages.” Once you have located the plugin, click the “Install Now” button.


2 Answers

There is not a show_post() function per se in WordPress core but it is extremely easy to write:

function show_post($path) {   $post = get_page_by_path($path);   $content = apply_filters('the_content', $post->post_content);   echo $content; } 

Note that this would be called with the page's path, i.e.:

<?php show_post('about');  // Shows the content of the "About" page. ?> <?php show_post('products/widget1');  // Shows content of the "Products > Widget" page. ?> 

Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.

Also, and no slight meant to @kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.

like image 58
MikeSchinkel Avatar answered Sep 21 '22 21:09

MikeSchinkel


I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.

function get_post_page_content( $atts ) {         extract( shortcode_atts( array(             'id' => null,             'title' => false,         ), $atts ) );          $the_query = new WP_Query( 'page_id='.$id );         while ( $the_query->have_posts() ) {             $the_query->the_post();                 if($title == true){                 the_title();                 }                 the_content();         }         wp_reset_postdata();      }     add_shortcode( 'my_content', 'get_post_page_content' ); 

For the shortcode,

[my_content id="Enter your page id number" title=Set this to true if you want to show title /] 
like image 33
Kit Johnson Avatar answered Sep 20 '22 21:09

Kit Johnson