【问题标题】:Wordpress Template System not working with get_template_partWordpress 模板系统不适用于 get_template_part
【发布时间】:2013-10-05 02:30:17
【问题描述】:

我正在设计一个使用 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文件中Projects中的P小写,但是标题后面的内容就消失了。

single.php 和 content-single.php 使用相同的方法,并且适用于该对。谁能给我一些关于我做错了什么的见解?

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

感谢您的时间和帮助。

【问题讨论】:

    标签: php wordpress templates wordpress-theming


    【解决方案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' );)都应该是小写的

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

    【讨论】:

    • 感谢您的意见!我进行了这些更改(将项目更改为小写,然后在函数文件中也进行了更改)。但是现在当你进入一个项目页面时,只显示标题:onedirectionconnection.com/tester/projects/take-me-home-tour
    • 也许是永久链接...您的意思是在设置>>永久链接下更改后端的永久链接吗?如果是这样,您指的是哪种永久链接结构?
    • 您不必更改永久链接结构,只需在“设置”->“永久链接”中重新保存即可。
    • 我已经重新保存了永久链接,但仍然是同样的问题(仅显示标题)。
    • &lt;main id="main" class="site-main" role="main"&gt; 之后的某个地方出现 PHP 错误,整个模板在那里被切断。您可以通过将error_reporting(E_ALL); 添加到您的functions.php 顶部,就在&lt;?php 调用下方来启用错误报告,然后检查是否打印了任何错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多