gpt4 book ai didi

WordPress 模板文件中的 PHP if/else 结构不起作用

转载 作者:行者123 更新时间:2023-12-02 07:39:55 24 4
gpt4 key购买 nike

关于 WordPress 模板文件中一段 PHP 代码的问题。

模板包含这段代码:

<h1><?php the_title(); ?></h1>

我希望仅当标题不是“Home”时才打印标题。

但是这段代码不起作用:

<?php if (the_title()!='Home'); ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>

最佳答案

the_title() echo ,它不返回它的标题。

使用get_the_title()相反。

<?php if (get_the_title() != 'Home'): ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>

顺便说一句,您似乎在尝试检测您是否在主页上。检查标题可能很不稳定,因为它可能会改变。

使用is_home()相反。

<?php if ( ! is_home()): ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>

关于WordPress 模板文件中的 PHP if/else 结构不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12263027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com