我有两个 Wordpress 模板
在某些情况下,我想从标题中传递一个“hideContent”变量,如果它设置为 true,则隐藏内容。
-content.php 文件
<div class="header">
<?php require locate_template("the-header.php"); <?php } ?>
</div>
<div class="content">
<?php if ($hideContent != true) { ?>
/* content to hide if flag is set */
<?php } ?>
</div>
如果我这样做,这是否只会影响对标题/内容的这个单一调用,还是会为后续的帖子/请求保持设置?
谢谢!
詹姆士
locate_template()
仅返回具有最高优先级的模板的文件名(以及路径),然后您将其作为常规文件包含在内。
这意味着,与 不同get_template_part()
,任何变量都与父模板文件共享范围,反之亦然,因此$hideContent
可以访问。
我建议你另外做的是纠正这部分:
<?php if ($hideContent != true) { ?>
进入
<?php if ($hideContent != true && isset($hideContent)) { ?>
,
或者总是设置这个变量。
此外,您可以添加条件,如果它是单个帖子 ( is_single()
, 然后检查帖子类型 ( global $post
, $post->post_type
) 等,目的是区分各种情况
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句