2012-07-11 49 views
0

我在其他功能中使用此功能工作,但它似乎並沒有在這個特殊的工作...無法獲取「get_the_title」在

<?php 
     $page = get_the_title(); 
     $blogusers = get_users('orderby=display_name'); 
     foreach ($blogusers as $user) { 
     $cpt_count = wpse31443_author_has_custom_post_type($user->ID, $page); 

     if (!empty($cpt_count)) { 
      echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>'; 
     } 
     } 
    ?> 

如果我改變$page = get_the_title();$page = 'title';然後它工作,所以這是與get_the_title();但我不確定是什麼因爲它在其他功能中起作用。

+0

你可以重複了'$ page',值,看看是否有在那裏實際上是什麼? – andrewsi 2012-07-11 18:50:52

+0

正在使用哪個頁面?它在循環中嗎?你應該看到帖子/頁面的標題嗎?如果它不在循環中,那麼你必須通過帖子ID - http://codex.wordpress.org/Function_Reference/get_the_title – matthewpavkov 2012-07-11 18:58:05

回答

2

「get_the_title()」不工作的最常見原因是它不在「循環」中。確保只從循環中調用該函數。如果從別處調用,則需要將頁面/帖子ID傳遞給該函數。

你會得到這裏的更多信息: http://codex.wordpress.org/Function_Reference/get_the_title

+0

這實際上並不適合我,但你讓我朝着正確的方向前進。這是一個愚蠢的錯誤,因爲帖子類型以小寫字母開頭,頁面以大寫字母開頭。謝謝! – 2012-07-11 19:23:29

+0

如果有人想知道,這個伎倆... '\t \t \t $ page = get_the_title(); \t $ page = strtolower($ page); \t' – 2012-07-11 19:27:17

2

試試這個:

<?php 
    global $post; 

    $page = $post->post_title; 
    $blogusers = get_users('orderby=display_name'); 
    foreach ($blogusers as $user) { 
    $cpt_count = wpse31443_author_has_custom_post_type($user->ID, $page); 

    if (!empty($cpt_count)) { 
     echo '<li>' . $user->display_name . '' . $cpt_count1 . '</li>'; 
    } 
    } 
?> 
+1

謝謝。這實際上完全是另一個問題,但如果它在循環之外,這會有所幫助。謝謝! – 2012-07-12 14:58:00