WordPress模板系统无法使用get_template_part

时间:2013-10-05 02:30:17

标签: php wordpress templates wordpress-theming

我正在使用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如您所见,我上面粘贴的模板未被使用。

另外,我尝试在single-projects.php文件中对项目中的P进行小写,但是标题之后的内容就会消失。

single.php和content-single.php使用相同的方法,它适用于该对。有人能够让我对我做错了什么有所了解吗?

如果您需要更多信息或代码,请与我们联系!

感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

首先要做的几件事:

register_post_type($post_type, $args)

  

$ post_type   (字符串)(必填)帖子类型。 (最多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' );

另外,请确保在更改自定义帖子类型初始化代码后更新永久链接结构(只需重新保存)。