2

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

该模板包含以下代码:

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

我希望仅在标题不是“主页”时才打印标题。

但是这段代码不起作用:

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

4 回答 4

5

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; ?>
于 2012-09-04T12:00:46.243 回答
1

<?php if (the_title()!='Home'): ?>

                              ^

使用:代替;

关联

于 2012-09-04T12:01:19.253 回答
1

或者你可以使用

http://codex.wordpress.org/Function_Reference/is_front_page

于 2012-09-04T12:07:05.207 回答
0

另一个简单的解决方案:

<?php if (the_title()!='Home') { ?>
   <h1><?php the_title(); ?></h1>
<?php } ?>
于 2012-09-04T13:52:47.833 回答