Better – Insert Page Content –

https://stackoverflow.com/questions/4090698/wordpress-include-content-of-one-page-in-another

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

[code style=”php”]
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters(‘the_content’, $post->post_content);
echo $content;
}
[/code]

Note that this would be called with the page’s path, i.e.:
[code style=”php”]
<?php show_post(‘about’); // Shows the content of the "About" page. ?>
<?php show_post(‘products/widget1’); // Shows content of the "Products > Widget" page. ?>
[/code]

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.


https://premium.wpmudev.org/blog/insert-wordpress-page-content-anywhere-you-like-in-your-site/


How To Do This

First, you’ll need to create a Page (not a Post) with the content want in it. You will then need to find the ID of the Page. If you aren’t sure how to do that, here’s one way.

Next, put the following code where you’d like your Page content to show up (such as in a sidebar):

<?php
 $id = ID#;
 $p = get_page($id);
 echo apply_filters('the_content', $p->post_content);
 ?>

You will need to insert your actual Page ID in the spot that says ID# above. So, for example, if I find out that my page ID is 14, my code in that section will look like this:

$id = 14;

After that, you may need to do some styling, depending on your layout and how you want things to work, but that’s it really. You can now go back and edit the page you created, and it will change the content wherever you’ve put your code for it.


Taking It a Little Further

I found the code above recently when I was looking to do something similar, though my situation was just slightly more complicated. I’ll go ahead and go over my situation as it may spur ideas for you to use this type of code in your own ways.

In my situation, I needed to put different content into different category templates.

In other words, I needed to be able to write up some editable content and images, and then insert them in a special place on my Blue Category page, for example. Then I needed to do the same thing for my Red Category, Yellow Category, and Green Category Pages.

I’ll try to represent that graphically.

 

 

The Solution

The solution was to use the code mentioned at the beginning of the post but to also use category templates. You can learn about creating category templates here.

As demonstrated above, I created a Page and then got the ID for it. (Let’s say the ID was 5.) This was content for my Blue Category.

I then created a Blue Category template (category-blue.php), placed the code above into the template where I wanted the content to appear, and put my Page ID into the correct spot so it looked like this:

<?php
 $id = 5;
 $p = get_page($id);
 echo apply_filters('the_content', $p->post_content);
 ?>

 

And that was it. My Blue Category was set.

Then I moved on and made my Red Category content by creating a NEW Page. I got the ID for that (ID = 6). And then I created a Red Category template (category-red.php) and put my code in. It looked like this:

<?php
 $id = 6;
 $p = get_page($id);
 echo apply_filters('the_content', $p->post_content);
 ?>

 

And on and on I went for all the different categories I wanted this for. Now, as you can see, I can easily go into my newly created Pages and update/change the inserted content for different categories.

If I want to change the inserted content on my Blue Category page, I just go to the Page I created for that and work in the editor, which is much easier than digging into the category templates and working with raw HTML, for example. In the editor, it’s easy to insert pretty much whatever you like – text, images, videos, etc.

So there you go — next time you wish you had an easily editable area somewhere on your site, you might remember this little trick of pulling in Page content wherever you like.

Scroll to Top