Include a specific page in your template

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.

2 Answers
2

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="https://wordpress.stackexchange.com/questions/26071/<?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="https://wordpress.stackexchange.com/questions/26071/<?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="https://wordpress.stackexchange.com/questions/26071/<?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!

Leave a Comment