is_page_template检查包含的文件

时间:2014-01-15 19:02:36

标签: wordpress templates

我需要检查我的自定义页面模板是否为index-with-sidebar.php

我这样做

function index_with_sidebar() {
    echo (is_page_template('index-with-sidebar.php')) ? 'col-md-6' : 'col-md-4';
}

在content.php我有

<div class="item <?php index_with_sidebar(); ?> ">item</div>

在index-with-sidebar.php中,我通过以下方式获取内容帖子格式:

<?php get_template_part( 'content', get_post_format() ); ?>

当我在index-with-sidebar.php页面上时,上面的函数显示col-md-4而不是col-md-6。如果我将col-md-6硬编码到div.item它可以正常工作。似乎该函数没有回显正确的值。我究竟做错了什么?你能救我吗?

修改

在循环之前

<?php $g = index_with_sidebar(); ?>

在循环中

<?php $e = has_post_format() ? 'content-'.get_post_format().'.php' : 'content.php'; ?>
<?php require(locate_template( $e )); ?>

我可以在我的content-X.php模板中使用$ g变量:)

1 个答案:

答案 0 :(得分:0)

如果你在content.php中调用该函数,你的页面模板不是index-with-sidebar.php,所以输出 false 答案是正确的。

要获得 true ,您只需调用index-with-sidebar.php中的函数。

相关问题