php - 用于博客内容的 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,两者都返回正确的标题和图像
解决方案
因此,帖子页面之类的功能与标准页面略有不同。最有可能的是,您的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() 那样自动回显它的返回值,因此添加了回显。
推荐阅读
- react-native - 将条带连接集成到市场的反应本机应用程序中
- python - 循环以从给定列表中选择特定数字
- json - 如何在 swift 中向函数中的 JSON 添加参数
- android - 如何调整电视视图的大小?
- postgresql - Postgres解释计划大量缓冲区共享读取,对于非常慢的行数
- javascript - 如何在 app.get 之外使用 req.user?
- reactjs - 使用钩子在反应中设置菜单项类名
- java - 没有 BoxLayout 的 JComponent 的 AlignmentX 和 Y
- apache - 本地主机打开文件夹结构而不是主页 - WAMP
- python - 使用 groupby 实现 for 循环