php - 仅显示登录作者的帖子时,如何获取 a 标签来包装 the_title?
问题描述
我几乎在那里,但在最后一块需要帮助。我试图在他们登录后仅显示作者帖子的链接。它可以工作,但下面的代码片段将帖子标题显示为纯文本,然后在<a>
帖子标题之后生成一个空标签,而不是用<a>
. 中的链接<a>
是正确的,只是没有包装帖子标题。我错过了什么?
<?php
$user_id = get_current_user_id();
$args=array(
'post_type' => 'team_fundraiser',
'post_status' => 'published',
'posts_per_page' => 1,
'author' => $user_id
);
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
$team_link .= '<a href="' .get_permalink(). '">'.the_title().'</a>';
endwhile;
echo $team_link;
?>
输出看起来像这样......
<div>
"My Awesome Post"
<a href="http://localhost/mysite/my-awesome-post/"></a>
</div>
我如何获得<a>
包装帖子标题?
解决方案
您需要替换the_title
为get_the_title
. the_title默认回显标题,因此标题出现在链接之前。get_the_title会将标题作为值返回给您。
所以将while循环内的行改为这个
$team_link .= '<a href="' . get_permalink(). '">'. get_the_title() . '</a>';
我还建议将函数包装在get_the_title
函数中esc_html
(WP Code Reference)和esc_url
函数中的超链接(WP Code Reference),以确保您不会得到任何与标题和链接一起输出的 HTML/JS。
最终会是这样:
$team_link .= '<a href="' . esc_url( get_permalink() ). '">'. esc_html( get_the_title() ) . '</a>';
推荐阅读
- typescript - nestjs 单元测试 createTestingModule 依赖注入
- javascript - 从检查元素是否作为另一个子元素存在于 Javascript 中删除错误
- excel - 如何将文本转换为列并忽略一些分隔符
- django - Django Ckeditor 将richtextfield中的不完整数据保存到cookies或服务器
- python - scrapy CrawlSpider 不遵循带有restrict_xpaths 的链接
- r - R中的抽象文本摘要
- javascript - MVC 交叉点观察者结构
- javascript - 具有内部异步代码和递归 EventListener 创建的 EventListener 的生命周期
- java - C# 和 Java 之间的 Diffie-Hellman 算法
- google-apps-script - 来自 Google Apps 脚本的 LDAP 搜索(最好在表格中)?