Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How do you include specific pages into a template?

Like for example on my websites main page I want to have about 3 different content areas that the client can regularly update as he needs to. I figure maybe the best way to do this is to just nest pages into the template and he can edit the page.

How can I set this up? The wordpress codex, the wordpress forum, and google have all failed to give me ANY solution.

I know there is a plugin that allows this but its not compatible with 3.1 and above. I dont like the idea of using a plugin anyhow, I figured that wordpress would have a simple template tag system setup for this, wrong.... PHP is probably where the answer lays.

Thanks for your help

p.s. also if you know of a better way to go about this other than using nested pages then please let me know. I'm just a wordpress noob and I figured this would be a straightforward way to accomplish the task.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Another great way of accomplishing something like this would be taking advantage of WordPress Custom Post Types.

You could set up 3 different types of features for easy use in the WordPress back-end.

First, Set up the Custom Post Types

To do this, open up your theme's functions.php file and include something like this:

<?php
function custom_register_post_types() {
    $labels = array(
        'name' => _x('People', 'post type general name'),
        'singular_name' => _x('Person', 'post type singular name'),
        'add_new' => __('Add New'),
        'add_new_item' => __('Add New Person'),
        'edit_item' => __('Edit Person'),
        'new_item' => __('New Person'),
        'view_item' => __('View Person'),
        'search_items' => __('Search People'),
        'not_found' => __('No People found'),
        'not_found_in_trash' => __('No People found in Trash'),
        'menu_name' => 'People'
    );
    $rewrite = array(
        'slug' => 'people'
    );
    $supports = array('title','editor');
    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_menu' => true,
        'query_var' => 'people',
        'rewrite' => $rewrite,
        'has_archive' => true,
        'hierarchical' => false,
        'supports' => $supports
    );
    register_post_type('people',$args);
    $labels = array(
        'name' => _x('Places', 'post type general name'),
        'singular_name' => _x('Place', 'post type singular name'),
        'add_new' => __('Add New'),
        'add_new_item' => __('Add New Place'),
        'edit_item' => __('Edit Place'),
        'new_item' => __('New Place'),
        'view_item' => __('View Place'),
        'search_items' => __('Search Places'),
        'not_found' => __('No Places found'),
        'not_found_in_trash' => __('No Places found in Trash'),
        'menu_name' => 'Places'
    );
    $rewrite = array(
        'slug' => 'places'
    );
    $supports = array('title','editor');
    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_menu' => true,
        'query_var' => 'places',
        'rewrite' => $rewrite,
        'has_archive' => true,
        'hierarchical' => false,
        'supports' => $supports
    );
    register_post_type('places',$args);
    $labels = array(
        'name' => _x('Things', 'post type general name'),
        'singular_name' => _x('Thing', 'post type singular name'),
        'add_new' => __('Add New'),
        'add_new_item' => __('Add New Thing'),
        'edit_item' => __('Edit Thing'),
        'new_item' => __('New Thing'),
        'view_item' => __('View Thing'),
        'search_items' => __('Search Things'),
        'not_found' => __('No Things found'),
        'not_found_in_trash' => __('No Things found in Trash'),
        'menu_name' => 'Things'
    );
    $rewrite = array(
        'slug' => 'things'
    );
    $supports = array('title','editor');
    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_in_menu' => true,
        'query_var' => 'things',
        'rewrite' => $rewrite,
        'has_archive' => true,
        'hierarchical' => false,
        'supports' => $supports
    );
    register_post_type('things',$args);
}
add_action('init', 'custom_register_post_types', 0);
?>

I have written a reference on the specifics of what is going on with this code here. You can also look into it on the codex.

What this will do is set up 3 new menus in the main left hand navigation of WordPress giving each of these types their own space in the interface. In this case, People, Places, and Things. (customize logically to suit your needs) Right now, these are set up to only show a Title bar and an Editor pane.

Query the Post Types for use on your homepage

I like to set up new query objects in order to keep everything nice and separated. Plus, you can call the objects in and out of other loops, which can be handy sometimes. Mix these (or something customized to suit your needs) into your templates to bring them into the desired pages.

<?php
$args = array( 'post_type' => 'people', 'numberposts' => '1' );
$people = new WP_Query($args);
if ( $people->have_posts() ) : 
    while ( $people->have_posts() ) : 
        $people->the_post(); 
        global $more;
        $more = 0;
?>

<div id="people-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( 'Edit', '<h5 class="edit-link">', '</h5>' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>

<?php
$args = array( 'post_type' => 'places', 'numberposts' => '1' );
$places = new WP_Query($args);
if ( $places->have_posts() ) : 
    while ( $places->have_posts() ) : 
        $places->the_post(); 
        global $more;
        $more = 0;
?>

<div id="places-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( 'Edit', '<h5 class="edit-link">', '</h5>' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>

<?php
$args = array( 'post_type' => 'things', 'numberposts' => '1' );
$things = new WP_Query($args);
if ( $things->have_posts() ) : 
    while ( $things->have_posts() ) : 
        $things->the_post(); 
        global $more;
        $more = 0;
?>

<div id="things-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( 'Edit', '<h5 class="edit-link">', '</h5>' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>

Those are just some quick and dirty examples of the way you could set up the query and loop. These will just set up and display the most recent or top post from each of the Custom Post Types.

Further instruction on the WP_Query class can be found here. Everything else is just HTML and CSS.

Enjoy!

share|improve this answer
    
Wow thanks Drew for this detailed post! This is alot of great information for me. Might be a bit more than what I need for this particular job, but I think I can use something like this in the future for sure.... On a side note I might switch from including pages to including posts so that way I can use the edit_post_link, what do you think? –  randyttp Aug 16 '11 at 22:24
    
Oh, edit_post_link works for pages too, it shows up when the user is logged in. –  Drew Gourley Aug 16 '11 at 22:49
    
Ok thanks for your quick reply. I ended up using your wp_query() after all to call the page and display it with the edit link. Everything is working as intended!! One more question about these pages, since they are not pages to be visited but just chunks of content, is there a kosher way to make them invisible or sorted correctly? I already set the pages as private, but I'm not entirely sure what that accomplishes, besides the obvious. –  randyttp Aug 17 '11 at 0:13
    
A private page will keep those from being visible when not logged in, as far as I know. Instead, you could call that content and leave out the permalinks, and then create a template file for those pages which just contains a wp_redirect() to the homepage. That way if anybody does hit those page url's by chance, it would plunk them to the homepage (or whatever page you want the content to be viewable from) –  Drew Gourley Aug 17 '11 at 2:06
    
Very nice solution!! Can't thank you enough! This has been a big help. –  randyttp Aug 17 '11 at 5:08
add comment

Thinking about it as including pages is a little off. Think about it as retrieving and including page's content.

$page = get_page_by_title('About The Tests');
$content = apply_filters('the_content', $page->post_content);
echo $content;

Another way would be set up special sidebar for that and let client add/remove/content via widgets. But such can easily be overkill.

share|improve this answer
    
Awesome that works - get_page_by_title, who wouldve thought :) Is there a way using this simple method to add an "edit" link so when the client is logged in he can edit right from the front end? –  randyttp Aug 16 '11 at 22:20
1  
@randyttp front-end editing is not native stuff, there are some plugins for that around. Easier to link to admin, from quick search edit_post_link() seems fitting. –  Rarst Aug 16 '11 at 22:56
    
ok I didnt know edit_post_link would work for pages too. although the codex states it cannot be used outside of the loop, with your method of retrieving the page content how could I use the edit_post_link with that? thanks for your help! –  randyttp Aug 16 '11 at 23:40
    
nevermind good sir I ended up using wp_query(page_id) so that way I could utilize the edit link. Thanks for pointing me in the right direction! –  randyttp Aug 17 '11 at 0:11
    
@randyttp see its arguments - you can pass ID to it instead of using loop. A lot (but not all) things that work for posts will work for pages, because essentially page is just a native post type called "page". –  Rarst Aug 17 '11 at 6:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.