Insert WordPress Page Content Anywhere You Like in Your Site


If you need to insert content from a Page into other places in your site, such as in a sidebar or a category page, then that’s easily done with this little trick I discovered from Scott Nelle.

As Scott mentions, he does this for clients so that they can easily add content to a sidebar. He just creates a Page and labels it so that they know that’s the Page they need to edit to change their content area in the sidebar.

Pretty smart, if you ask me. They don’t need to know where the widgets are, for example, or even what a widget is. Just tell them, “Edit the ‘Sidebar Page’ if you want to change the content in this area.”

Featured Plugin - WordPress Appointments Plugin

Take, set and manage appointments and client bookings without having to leave WordPress. Appointments+ makes it easy.
Find out more

 

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.

 

Featured Plugin - WordPress Pop-Up Chat Plugin

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

 

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.

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Photo: Embedment from BigStock

Comments (19)

    • Yeah, that thought did cross my mind. If you could easily no-index/no-follow that page (which wouldn’t be linked to anyway), then you probably should. But to tell you the truth, I’ve stopped worrying about Google et al to such a small degree. I’ve seen them screw up too many big things to worry about the small things anymore. When they can’t get big things right, they don’t really deserve my attention on the small ones. That doesn’t mean I wouldn’t think about SEO and apply basic SEO, but you can’t let it keep you from making your site better for your visitors.

      • ok fair enough. you are right that the page will not be linked to anyways, so basically only robots and super-bored humans will be able to find the page :)

        The reason I asked, because I was thinking about sth remotely similar for a mobile child theme: to make one or two pages specifically for mobile and don’t let them show up anywhere else on the desktop site.

        Anyways, cool tip and indeed is probably a lot easier for clients to understand than widgets.

          • Joe – I agree with you, if creating a WP plugin that has very low over head would do the trick I am all for it, it should save a minute or two. But I personally encourage as few plugins as possible to keep your site loading as fast as possible.

            Creating a robot.txt file or updating an existing one is a pretty straight forward thing to do and has no performance degradation possibilities, as a plugin could possibly have.

    • Thomas – Pointing you to a site wouldn’t do any good. The content just shows up on the page like any other content.

      If you understand text widgets, then think of it like a text widget. But there are a few differences/advantages. One advantage is that you are using the regular wordpress editor to write the content. This is easier than messing with HTML, for example. If you wanted to put an image into a text widget, you would need to do it with HTML.

      The other advantage is that your content will show up wherever you place the code in your templates. You could make a text widget do that, for example, but it would take extra work, and as mentioned, you would still be limited by not having an easy-to-use editor.

      Look at my diagram above again. You’ll see that I’m adding content into category pages. Normally, you can’t add (and edit) original content in category pages. Normally category pages show the posts from that category and then other things that you have in your theme — maybe a sidebar with recent posts, etc. But with this solution, I can make a spot on one or all of my category pages that will be filled with whatever I want to put in it — text, images, videos, etc. And I can also change that content easily by simply editing the page that I used to create the content.

  1. One aspect of this solution that should be mentioned is: Unless your theme has been taught to do otherwise, it will display those pages created to hold sidebar content in any site-internal search query with matching keywords.

    If that is a desired side-effect, your method will work just fine; if not, you could use a plugin like Custom Post Widget. It does basically the same, only it provides a Custom Post Type for your custom content (instead using a core post type like pages) and a widget to display that content in any widget-ready area on your site.
    You can fully use the visual editor to create your sidebar content just like if you were creating a regular page or post. The main difference is:
    The plugin then prevents your custom content from showing up in any other place than the widget shipped with the plugin. That means, your custom content will explicitly *not* show up in site-internal search queries—which can be good or bad depending on your particular scenario.

    Personally, I like your approach a lot, because it leverages core features in a creative way. It’ll do just fine for most single user sites where you need a quick solution and want to keep your content usable with WordPress core features.

  2. I like this workaround a lot. It can be difficult for non-technical users to edit text widgets.

    Very recently, however, I have been exploring best practices when it comes to theme development, and it seems that it might be a better idea to develop this as a widget/plugin. That way the functionality isn’t tied to the theme (which is really intended for presentation of the data, rather than functionality).

    I visualize a plugin that creates a CPT and a widget that displays a drop-down of posts in that CPT. You add the widget and pick the post to add the content to the page. This would be much more powerful – as the customer could then easily change what they show AND where it shows.

  3. i’m pretty New to wordpress a beginner i too had the same scenario and thought of the similar result but was just thinking of whether it was a good practice now i shall do it this way

Participate