1

I am designing a wordpress theme using the Underscore theme as a base. I have created a custom post type called Projects. Here's the code that registers the post type:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'Projects',
        array(
            'labels' => array(
                'name' => __( 'projects' ),
                'singular_name' => __( 'projects' )
            ),
        'taxonomies' => array('post_tag'),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title','editor','thumbnail','excerpt'),
        )
    );
}

So, now that I created a new post type, I went to go design the template for the post type. Since Underscore uses get_template_type to pull the theme together, I made one file called single-projects.php which includes the following code:

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package professional1d
 */

get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', 'Projects' ); ?>

                <?php professional1d_content_nav( 'nav-below' ); ?>

                <?php
                    // If comments are open or we have at least one comment, load up the comment template
                    if ( comments_open() || '0' != get_comments_number() )
                        comments_template();
                ?>

            <?php endwhile; // end of the loop. ?>

        </main><!-- #main -->
    </div><!-- #primary -->
</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

And then I created the content-projects.php page which has the code for the actual template, which is:

<?php
/**
 * @package professional1d
 */
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <header class="entry-header">
        <h1 class="entry-title"><?php the_title(); ?></h1>

        <div class="entry-meta">
            <?php professional1d_posted_on(); ?>
        </div><!-- .entry-meta -->
    </header><!-- .entry-header -->

    <div class="entry-content">
        <p>
            <img src="<?php
                $thumb_id = get_post_thumbnail_id();
                $thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
                echo $thumb_url[0];
                        ?>" class="img-thumbnail" align="left">
            <strong><u><?php echo get_the_title(); ?></u></strong><br>
            <strong>Date: </strong> 
                <?php the_field('start_date'); ?>
                <?php
                    if(get_field('end_date')){
                        echo ' - ' . get_field('end_date') .;
                    }
                ?><br>
            <strong>Type: </strong> <?php the_field('type'); ?><br>
            <strong>Status: </strong> <?php the_field('status'); ?>
        </p>
        <div class="sexy_line"></div>

        <?php the_content(); ?>
        <?php
            wp_link_pages( array(
                'before' => '<div class="page-links">' . __( 'Pages:', 'professional1d' ),
                'after'  => '</div>',
            ) );
        ?>
    </div><!-- .entry-content -->

    <footer class="entry-meta">
        <?php
            /* translators: used between list items, there is a space after the comma */
            $category_list = get_the_category_list( __( ', ', 'professional1d' ) );

            /* translators: used between list items, there is a space after the comma */
            $tag_list = get_the_tag_list( '', __( ', ', 'professional1d' ) );

            if ( ! professional1d_categorized_blog() ) {
                // This blog only has 1 category so we just need to worry about tags in the meta text
                if ( '' != $tag_list ) {
                    $meta_text = __( 'This entry was tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'professional1d' );
                } else {
                    $meta_text = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'professional1d' );
                }

            } else {
                // But this blog has loads of categories so we should probably display them here
                if ( '' != $tag_list ) {
                    $meta_text = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'professional1d' );
                } else {
                    $meta_text = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'professional1d' );
                }

            } // end check for categories on this blog

            printf(
                $meta_text,
                $category_list,
                $tag_list,
                get_permalink(),
                the_title_attribute( 'echo=0' )
            );
        ?>

        <?php edit_post_link( __( 'Edit', 'professional1d' ), '<span class="edit-link">', '</span>' ); ?>
    </footer><!-- .entry-meta -->
</article><!-- #post-## -->

</div>

I think I followed the correct protocol to make template work, but for some reason, when I open one of the Project posts, a page with the content of the post does appear, but it's not using the template I designed in the content-projects.php file. You can see the issue in action here: http://onedirectionconnection.com/tester/?projects=take-me-home-tour As you can see, the template I pasted above is not being used.

Also, I tried lowercasing the P in Projects in the single-projects.php file, but then the content after the header just disappears.

The single.php and content-single.php use the same method and it's working for that pair. Would anyone be able to offer me some insight into what I am doing wrong?

Let me know if you need any more information or code!

Thanks for your time and help.

4

1 回答 1

1

A few things first:

register_post_type($post_type, $args):

$post_type (string) (required) Post type. (max. 20 characters, can not contain capital letters or spaces) Default: None

Given that, your

register_post_type( 'Projects',...

should be

register_post_type( 'projects',...

You can use the labels array to create properly capitalized names.

'labels' => array(
    'name' => __( 'Projects' ),
    'singular_name' => __( 'Project' )
),

Your custom template file names should all be lowercase as well as your get_template_part calls like get_template_part( 'content', 'projects' );

Also make sure you update your permalinks structure (just re-save) once you made changes to your custom post type init code.

于 2013-10-05T05:25:30.823 回答