【问题标题】:WordPress: How To Display the_title as a LinkWordPress:如何将 the_title 显示为链接
【发布时间】:2014-04-06 14:35:54
【问题描述】:

我无法在 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>"; 
    }
}

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

如果我这样编码:

echo '<li><a href='.get_permalink().'>'.the_title().'</a></li>';

它将显示标题,但不会显示为链接。

有什么想法吗?您的帮助将不胜感激。

谢谢!

【问题讨论】:

  • @cale_b 您的回答部分正确。在我的一次编辑中,我有双引号,但有些我忘了把它们放回去。这是正确的答案:stackoverflow.com/a/22182574/742030
  • the_title() 是自回显的,它会自己回显,所以当在回显中使用时它会中断,如果在回显语句中使用,请使用get_the_title()

标签: php wordpress hyperlink


【解决方案1】:

the_title 输出内容本身。您需要使用返回内容的get_the_title()

试试这个:

echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';

【讨论】:

  • 谢谢!我有那个 b4 但我错过了双引号。
  • 它结合了双引号 - 必须再等 2 分钟 b4 我可以检查它作为答案 - Thnx Again!
【解决方案2】:

我认为您只需要为href="" 使用双引号即可让链接正常工作

echo '<li><a href="'.get_permalink().'">'.the_title().'</a></li>';

Learn more here

【讨论】:

  • 我提高了你,因为我确实需要双引号 - 但是,我最初确实有它,但它没有用。
  • @Andaero 尝试echo get_permalink(); 是否正确打印链接?
  • the_title() 是自回显的。
【解决方案3】:

您在该行缺少右引号。

注意添加的右引号:

    while($query->have_posts()) : $query->the_post();
        echo '<li><a href=' . get_permalink() . '>' . get_the_title() . '</a></li>';
    endwhile;

此外,您的标记应包括链接 url 周围的引号,如下编辑:

echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';

【讨论】:

  • 我提高了你,因为我确实需要双引号 - 但是,我最初确实有它,但它没有用。我重试了你的两个答案,但都没有成功。
  • the_title() 是自回显的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-20
  • 2011-07-01
  • 2012-02-01
  • 1970-01-01
  • 2022-12-06
  • 2011-11-11
相关资源
最近更新 更多