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.

I wrote a simple plugin to create a custom post type named "people":

add_action( 'init', 'create_person' );

function create_person() {
    register_post_type( 'people',
        array(
            'labels' => array(
                'name' => 'People',
                'singular_name' => 'Person',
                'add_new' => 'Add New',
                'add_new_item' => 'Add New Person',
                'edit' => 'Edit',
                'edit_item' => 'Edit Person',
                'new_item' => 'New Person',
                'view' => 'View',
                'view_item' => 'View Person',
                'search_items' => 'Search People',
                'not_found' => 'No person found',
                'not_found_in_trash' => 'No people found in trash',
                'parent' => 'Parent Person'
            ),

            'public' => true,
            'menu_position' => null,
            'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
            'taxonomies' => array( '' ),
            'menu_icon' => plugins_url( 'images/people_icon.png', __FILE__ ),
            'publicly_queryable' => true,
            'show_ui' => true, 
            'show_in_menu' => true, 
            'query_var' => true,
            'rewrite' => array( 'slug' => 'people','with_front' => FALSE),
            'capability_type' => 'post',
            'hierarchical' => false,
            'has_archive' => true
        )
    );
}

And added a template called single-person.php to display the custom post type. This filter is in the same file as the code above:

add_filter( 'template_include', 'include_template_people', 1 );

function include_template_people( $template_path ) {
    if ( get_post_type() == 'people' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-people.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-people.php';
            }
        }
    }
    return $template_path;
}

Then there is the actual template here:

<?php
 /*Template Name: New Template
 */

get_header(); ?>
<div id="primary">

    <div id="content" role="main">
    <?php
    $mypost = array( 'post_type' => 'people', );
    $loop = new WP_Query( $mypost );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <div class="person">
                <div class="person-header">
                    <div class="person-image">
                        <?php the_post_thumbnail( array( 300, 300 ) ); ?>
                    </div>
                    <div class="person-name"><?php the_title(); ?></div>
                    <div class="roles-list">
                        <?php echo get_the_term_list( $id, 'roles', '', ', ', '' ); ?>
                    </div>
                </div>
                <div class="person-content"><?php the_content(); ?></div>
            </div>
        </article>

    <?php endwhile; ?>
    </div>
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>

UPDATED:

I changed the question a bit. I removed a piece of code that I had added. The problem with this template is that when I go to the URL:

mysite.com/people/firstname-lastname

I expect to only get the post of type 'people' which has the slug "firstname-lastname". Instead, all the posts of type 'people' show up on the page. How can I change this so that only the single 'post' is displayed?

share|improve this question
2  
Which is the value of $id in args array? –  iEmanuele Jul 30 '13 at 15:30
2  
What does post ID 221 correspond to? Check the database for that ID. –  s_ha_dum Jul 30 '13 at 15:31
    
221 corresponds to a "post" that is unrelated. –  KevinM Jul 30 '13 at 19:06
    
iEmanuele, I'm not sure I understand your question. 221 is the value $id is set to after the loop is over. Inside the loop it should be set to 106, but apparently it is empty inside the loop. –  KevinM Jul 30 '13 at 19:11
    
in $mypost = array( 'post_type' => 'people', 'post__in' => array( $id ) ); where the $id variable come from? –  G. M. Jul 30 '13 at 20:04

1 Answer 1

up vote 1 down vote accepted

If the code for the 'actual template' you posted is in the single-people.php...

you do not need any query at all!

When you call the url mysite.com/people/firstname-lastname wordpress already know that you want to view that person, only search for a file that display it.

Normally, wordpress, in this case, search for the file single-people.php in the theme folder and if don't find it, than search for single.php and so on, following Template Hierarchy.

Your plugin modify this behavior, so if no single-people.php file is the theme folder, instead of searching for single.php in theme now wordpress search for single-people.php in your plugin folder.

So, again, if the file come from single-people.php you have not to put here any query at all to use the standard main wp query that is, in this case, just showing the post.

<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php if ( have_posts() ) : the_post(); ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <div class="person">
            <div class="person-header">
                <div class="person-image">
                    <?php the_post_thumbnail( array( 300, 300 ) ); ?>
                </div>
                <div class="person-name"><?php the_title(); ?></div>
                <div class="roles-list">
                    <?php echo get_the_term_list( get_the_ID(), 'roles', '', ', ', '' ); ?>
                </div>
            </div>
            <div class="person-content"><?php the_content(); ?></div>
        </div>
    </article>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
share|improve this answer
    
That worked! Thank you very much. I knew there was some basic thing I was missing. –  KevinM Jul 31 '13 at 0:14

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.