WordPress主题的条件模板标签

时间:2012-06-17 15:51:21

标签: wordpress wordpress-theming

我无法创建有条件地显示某些内容的模板。这适用于包含文章和教程的博客。教程将以一个包含有关教程的其他信息的框开头。额外信息输入自定义字段。

在我的“single.php”文件中,我有以下代码:

<section class="entry-content group">
    <?php if ( in_category('tutorials') ) { echo '
        <div class="tutorial-info-box group">
            <h5>About This Tutorial</h5>
            <ul class="tutorial-stats">
                <li> <span> Difficulty: </span> </li>
                <li> <span> Prerequisites: </span> </li>
                <li> <span> Time required: </span> </li>
                <li> <span> Will improve your knowledge of: </span> </li>
            </ul>
        </div>
    '; } ?>
    <?php the_content(); ?>
</section>

上述方法适用于仅在“教程”类别中的帖子上呈现此框。当我尝试从自定义字段中包含数据时,我遇到了麻烦:

<section class="entry-content group">
    <?php if ( in_category('tutorials') ) { echo '
        <div class="tutorial-info-box group">
            <h5>About This Tutorial</h5>
            <ul class="tutorial-stats">
                <li> 
                    <span> Difficulty: </span>
                    <?php get_post_meta( $post->ID, 'difficulty', true ); ?> 
                </li>
                <li> 
                    <span> Prerequisites: </span>
                    <?php get_post_meta( $post->ID, 'prerequisites', true ); ?>  
                </li>
                <li> 
                    <span> Time required: </span> 
                    <?php get_post_meta( $post->ID, 'time', true ); ?>
                </li>
                <li> 
                    <span> Will improve your knowledge of: </span>
                    <?php get_post_meta( $post->ID, 'improve', true ); ?>
                </li>
            </ul>
        </div>
    '; } ?>
    <?php the_content(); ?>
</section>

我最后只有一个空白的白色屏幕。我假设这是某种语法错误,但我无法弄清楚它是什么。有人可以帮忙吗?在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

您在字符串中包含一个开放的<?php。纠正它:

<section class="entry-content group">
    <?php if ( in_category('tutorials') ): ?>
        <div class="tutorial-info-box group">
            <h5>About This Tutorial</h5>
            <ul class="tutorial-stats">
                <li> 
                    <span> Difficulty: </span>
                    <?php echo  get_post_meta( $post->ID, 'difficulty', true ); ?> 
                </li>
                <li> 
                    <span> Prerequisites: </span>
                    <?php echo get_post_meta( $post->ID, 'prerequisites', true ); ?>  
                </li>
                <li> 
                    <span> Time required: </span> 
                    <?php echo get_post_meta( $post->ID, 'time', true ); ?>
                </li>
                <li> 
                    <span> Will improve your knowledge of: </span>
                    <?php echo get_post_meta( $post->ID, 'improve', true ); ?>
                </li>
            </ul>
        </div>
    <?php endif; ?>
    <?php the_content(); ?>
</section>