繁体   English   中英

仅显示已登录作者的帖子时,如何让 a 标签包装 the_title?

[英]How do I get the a tag to wrap the_title when showing only the logged-in author's post?

我几乎在那里,但在最后一块需要帮助。 我试图在他们登录后只显示指向作者帖子的链接。它有效,但下面的代码段将帖子标题显示为纯文本,然后在帖子标题后生成一个空的<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 代码参考)中,并将超链接esc_urlesc_url函数( WP 代码参考)中,以确保不会随标题和链接一起输出任何 HTML/JS。

最终会是这样:

$team_link .= '<a href="' . esc_url( get_permalink() ). '">'. esc_html( get_the_title() ) . '</a>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM