关于 WordPress 模板文件中的一段 PHP 代码的问题。
该模板包含以下代码:
<h1><?php the_title(); ?></h1>
我希望仅在标题不是“主页”时才打印标题。
但是这段代码不起作用:
<?php if (the_title()!='Home'); ?>
<h1><?php the_title(); ?></h1>
<?php endif; ?>
the_title()
回声,它不返回它的标题。
改为使用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; ?>
另一个简单的解决方案:
<?php if (the_title()!='Home') { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>