WordPress模板系统不适用于get_template_part


Wordpress Template System not working with get_template_part

我正在设计一个以下划线主题为基础的wordpress主题。我创建了一个名为"项目"的自定义帖子类型。下面是注册帖子类型的代码:

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'),
        )
    );
}

所以,现在我创建了一个新的帖子类型,我去设计帖子类型的模板。由于 Underscore 使用 get_template_type 将主题拉到一起,因此我创建了一个名为 single-project 的文件.php其中包含以下代码:

<?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(); ?>

然后我创建了内容项目.php页面,其中包含实际模板的代码,即:

<?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>

我想我遵循了正确的协议来使模板工作,但由于某种原因,当我打开其中一个项目帖子时,确实出现了一个包含帖子内容的页面,但它没有使用我在内容项目中设计的模板.php 文件。您可以在此处查看问题的实际情况: http://onedirectionconnection.com/tester/?projects=take-me-home-tour 如您所见,我上面粘贴的模板未被使用。

此外,我尝试在单项目.php文件中将项目中的 P 小写,但随后标题后面的内容就消失了。

单.php和内容单.php使用相同的方法,它适用于该对。有人能为我提供一些关于我做错了什么的见解吗?

如果您需要更多信息或代码,请告诉我!

感谢您的时间和帮助。

首先要做几件事:

register_post_type($post_type, $args)

$post_类型 (字符串((必填(帖子类型。(最多20个字符,不能包含大写字母或空格( 默认值:无

鉴于此,您的

register_post_type( 'Projects',...

应该是

register_post_type( 'projects',...

您可以使用 labels 数组创建大小写正确的名称。

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

自定义模板文件名应全部为小写,并且您的 get_template_part 调用应为小写,例如 get_template_part( 'content', 'projects' );

还要确保在对自定义帖子类型初始化代码进行更改后更新永久链接结构(只需重新保存(。