首页 > 解决方案 > 用于博客内容的 Home.php 无法检索页面标题 - Wordpress

问题描述

嗨,我想在我的 wordpress 主题中做博客部分,我设置使用静态首页和博客页面来显示最新帖子。

所以在我的 home.php 中我有:

<?php get_header(); ?>
<section class="header--page">
    <div class="header--img">
        <?php the_post_thumbnail() ?>
    </div>
    <div class="container">
        <h1><?php the_title() ?></h1>
    </div>
</section>
<?php get_footer(); ?>

但是不是从页面获取标题和从页面获取特色图片,而是显示第一篇文章中的内容,有什么想法吗?

我尝试我的 page.php 和 front-page.php,两者都返回正确的标题和图像

标签: phpwordpresswordpress-themingblogs

解决方案


因此,帖子页面之类的功能与标准页面略有不同。最有可能的是,您的the_title()函数正在返回要显示的第一篇文章的标题。

您需要做的是:

<h1><?php echo get_the_title(get_option('page_for_posts')) ?></h1>

需要发生的是您需要获取帖子页面的 ID,并使用该 ID 查找其标题。

因此,您需要改用get_the_title(),因为the_title()不允许您传递页面或帖子的 ID。get_the_title()接受 ID 作为参数。

可以使用get_option('page_for_posts') 获取您在 WP 设置 > 阅读中指定为帖子页面的页面 ID 。

因此,将它们放在一起,上面的代码片段将获得帖子页面的标题,并在屏幕上回显它。(get_the_title() 不会像 the_title() 那样自动回显它的返回值,因此添加了回显。


推荐阅读