2011-05-24 87 views
1

我想要使用2個變量獲得鏈接,但輸出是鏈接和標題,但沒有出現html/clickable鏈接。PHP語法問題

我得到的東西此鏈接:

http://www.mydomain.com/post1/post_title_here

下面是代碼:

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

誰能幫助嗎?

感謝

UPDATE:

這裏是整個代碼塊:

<div id="MyBlock1"> 
     <?php 
      $query = new WP_Query('posts_per_page=5'); 

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

      wp_reset_postdata(); 

     ?> 
    </div> 
+1

代碼對我來說很好。什麼是呈現? 'the_permalink()'的返回值是多少? – 2011-05-24 15:47:28

+0

粘貼永久鏈接函數中的代碼,以便我們知道它返回的內容 – amosrivera 2011-05-24 15:48:16

+0

the_permalink()包含鏈接。例如:http://www.mydomain.com/post1/ – Satch3000 2011-05-24 15:49:16

回答

5

這是因爲見codex reference wordpress函數the_permalink()the_title()顯示已經不需要回顯的相應結果。如果您想要返回值的函數,則必須使用get_permalink()get_the_title()

因此,無論做:

<div id="MyBlock1"> 
    <?php 
     $query = new WP_Query('posts_per_page=5'); 
     while($query ->have_posts()) : $query ->the_post(); 
      echo '<li>'; 
      echo '<a href="'.get_permalink().'">'.get_the_title().'</a>'; 
      echo '</li>'; 
     endwhile; 
     wp_reset_postdata(); 
    ?> 
</div> 

<div id="MyBlock1"> 
    <?php 
     $query = new WP_Query('posts_per_page=5'); 
     while($query ->have_posts()) : $query ->the_post(); 
      echo '<li><a href="'; 
      the_permalink(); 
      echo '">'; 
      the_title(); 
      echo '</a></li>'; 
     endwhile; 
     wp_reset_postdata(); 
    ?> 
</div> 

雙方將合作。

0

你需要絕對確保.the_title().是肯定bieng設置的值。如果不是,那麼將不會顯示HTML,因爲錨標記沒有文本。只是一個想法(我已經做了很多次,嘗試print_f();荷蘭國際集團的the_title()希望它幫助

+0

這兩個變量都是返回值。 – Satch3000 2011-05-24 15:56:32

2

下面是用於調試的清單:。

1)是the_title()返回一個空字符串? (你可以通過查看HTML源代碼來查看)

2.)你是否在身體標籤內回顯了這個內容?

3.)這是在一個隱藏的HTML元素回聲?

+0

1.是的。是的。 3.第 – Satch3000 2011-05-24 15:55:45

2
echo '<a href="'.the_permalink().'">'.the_title().'</a>'; 

在這種情況下,你需要使用get_permalink而不是the_permalinkget_the_title而不是the_title

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

WordPress的the_*功能直接做echo通話,而get_*函數返回,您可以使用作進一步處理,就像你在做串聯的值。

(也注意到不一致的命名約定 - 這可能是一個痛苦)

2

您可以使用相應的get_ *版本:

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

更多

+0

它實際上是'get_the_title'。 – ceejayoz 2011-05-24 15:53:54

+0

@ceejayoz:哎呀,你說得對,謝謝。更新。 – 2011-05-24 15:56:14