在 php 中显示依赖于页面的标题


Displaying header dependent on page in php

我不知道为什么这不起作用

<?php $title = the_title(); ?>
<h1 class="top-entry-title">
  <?php if( $title === "News" ): ?>
     <?php the_title(); ?>
   <?php endif; ?></h1>

只是显示每个页面的标题,即使它不等于新闻?

问题出在顶部。

$title = the_title();

将打印标题。另请参阅Wordpress Codex。

你的代码中没有echo(我认为它太复杂了):

<?php
    if( $title === "News" )
        echo "News";
?>

应该做这项工作。

你可以使用这个:

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

这会将标题作为 h1 打印到屏幕上。

在这里阅读 wordpress doc Codex