我怎样才能<div id=\'banner\'>
仅在索引中可见。php并将其隐藏在其他页面上,如single。WordPress中的php?我的div id=\'banner\'
位于我的页眉中,页眉显示在所有页面上get_header();
hide/show a div in wordpress
3 个回复
最合适的回答,由SO网友:Dmitry Mayorov 整理而成
检测索引。php可能不是最好的选择,因为WordPress在许多情况下都使用此模板文件。最终,所有页面都将返回到使用索引。php,如果没有其他模板文件与此页面关联。
因此,不要瞄准指数。php,熟悉template hierarchy 并尝试使用conditional tags, 只要可能。
例如,如果要仅在存档页上显示横幅,可以执行以下操作:
<?php if ( is_archive() ) : ?>
<div id="banner">
<!-- Banner content goes here. -->
</div>
<?php endif; ?>
类似地,可以组合条件标记:<?php if ( is_front_page() && is_home() ) : ?>
<div id="banner">
<!-- Banner content goes here. -->
</div>
<?php endif; ?>
这样,您的代码将经得起未来的考验,并符合WordPress编码标准。SO网友:Rishabh
您可以使用隐藏其他页面上的divis_home()
像这样的功能
<?php
is_home(){
?>
<div id="banner">
</div>
<?php
}
无论您在其中键入什么内容is_home()
车身(portion comes under {}
) 该内容仅在中可见homepage
.SO网友:leonardeveloper
你可以检查一下https://developer.wordpress.org/reference/functions/is_page/ 如果你想用WordPress来做的话。
if(is_page( 42 )) //This way it checks the `page id`
// show banner
else
// hide banner