首页 文章

WordPress模板系统无法使用get_template_part

提问于
浏览
1

我正在使用Underscore主题作为基础来设计wordpress主题 . 我创建了一个名为Projects的自定义帖子类型 . 这是注册帖子类型的代码:

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

然后我创建了 content-projects.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>

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

此外,我尝试在单个projects.php文件中降低项目中的P,但随后 Headers 后面的内容消失 .

single.php和content-single.php使用相同的方法,它适用于该对 . 有人能够给我一些洞察我做错的事吗?

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

谢谢你的时间和帮助 .

1 回答

  • 1

    先做几件事:

    register_post_type($post_type, $args)

    $ post_type(string)(必填)帖子类型 . (最多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' );

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

相关问题