2012-07-11 47 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