WordPress:如何将the_title显示为链接

3 php wordpress hyperlink

我在将标题显示为WordPress功能中的链接时遇到问题.

如果我这样编码:

function my_popular_posts($count) {
    $query = new WP_Query( array( 
    'orderby' => 'comment_count',
    'order' => 'DESC',
    'posts_per_page' => $count));
    if($query->have_posts()) {
        echo '<ul>';
        while($query->have_posts()) : $query->the_post();
**THIS LINE --> echo '<li><a href='.get_permalink().'> .the_title(). </a></li>';
        endwhile;
        echo '</ul>';
    } else {
        echo "<p>No popular posts found<p>"; 
    }
}
Run Code Online (Sandbox Code Playgroud)

在运行时,链接显示为".the_title()"

如果我这样编码:

echo '<li><a href='.get_permalink().'>'.the_title().'</a></li>';
Run Code Online (Sandbox Code Playgroud)

它将显示标题,但不显示链接.

有任何想法吗?我们将不胜感激.

日Thnx!

Rah*_*zir 5

the_title输出内容本身.你需要使用get_the_title()

试试这个:

echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
Run Code Online (Sandbox Code Playgroud)