2014-03-04 26 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