2014-03-04 22 views
3

我无法在WordPress功能中显示标题链接。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>'; 

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

任何想法?您的帮助将不胜感激。

Thnx!

+0

@cale_b你的答案是正确的patially。我在编辑过程中遇到过双引号,但是有些时候我忘了将它们放回原处。这是正确的答案:http://stackoverflow.com/a/22182574/742030 – CelticParser

回答

4

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

试试这个:

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

Thx!我有那个b4,但我错过了双引号。 – CelticParser

+0

它与双引号结合使用 - 必须再等2分钟b4我可以将它作为答案来检查 - Thnx Again! – CelticParser

1

您错过了关闭该行上的引号。

注意添加收盘报价:

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() . '">' . the_title() . '</a></li>'; 
+0

我提高了你的bcoz我确实需要双倍引号 - 然而,我确实有这个,并没有奏效。我重试了你的答案,但都没有奏效。 – CelticParser

1

我想你只需要使用双报价为href=""让链接工作

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

Learn more here

+0

我提高了你bcoz我确实需要双引号 - 但是,我确实有这个,并没有工作。 – CelticParser

+0

@Andaero尝试'echo get_permalink();'是否正确地打印链接? – Fabio