WordPress 中的条件语句

发布于 2024-09-01 08:56:50 字数 491 浏览 3 评论 0原文

为什么这段代码工作正常:

<?php if (**is_page**('4')) { ?> 
<style type="text/css"> 
body {background-image:url("<?php bloginfo('template_url'); ?>/images/bac4.jpg");} 
</style> 
<?php } else ?> 

而这则不行:

<?php if (**is_category**()) { ?> 
<style type="text/css"> 
body {background-image:url("<?php bloginfo('template_url'); ?>/images/coaching.jpg");} 
</style> 
<?php } else ?> 

Why this code works fine:

<?php if (**is_page**('4')) { ?> 
<style type="text/css"> 
body {background-image:url("<?php bloginfo('template_url'); ?>/images/bac4.jpg");} 
</style> 
<?php } else ?> 

and this not:

<?php if (**is_category**()) { ?> 
<style type="text/css"> 
body {background-image:url("<?php bloginfo('template_url'); ?>/images/coaching.jpg");} 
</style> 
<?php } else ?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-09-08 08:56:50

在我的模板的 header.php 中为我工作。在模板的 archive.php 中添加一些内容,最好是在 is_category() 内,以验证您是否位于类别页面上。另外,我发现替代 if 语法更容易阅读和调试,但是 YMMV。

if( is_category() ):
    ?>
    <!-- some html -->
    <?php
else:
    ?>
    <!-- other -->
    <?php
endif;

Works for me in my template's header.php. Add some stuff to your template's archive.php, preferably inside the is_category(), to verify you're on a category page. Also, I find the alternative if syntax easier to read and debug, but YMMV.

if( is_category() ):
    ?>
    <!-- some html -->
    <?php
else:
    ?>
    <!-- other -->
    <?php
endif;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文