2014-12-24 40 views
1

我想查詢我的WordPress的特定帖子類型「team_members」,然後將標題存儲在數組中。當我使用以下我的輸出只顯示0,1,2(我只有3個職位),它不顯示帖子標題。查詢WordPress的帖子,並把標題放在數組

$my_array=array(); 

// the query 
$args=array('post_type' => 'team_members','order'=>'ASC'); 
$my_query = new WP_Query($args); 

if ($my_query->have_posts()) : 
while ($my_query->have_posts()) : $my_query->the_post(); 
    $my_array[]=get_the_title(get_the_ID()); 
endwhile; 
endif; 

任何幫助或方向將不勝感激。

感謝 約翰

+0

你不需要'get_the_ID()',並在ORDER BY指定。 – vaso123

+0

我通過個人wordpress安裝運行此代碼。只需將帖子類型更改爲我的其中一個,並添加一個print_r($ my_array);在end和endif之間;如預期的那樣,它出現了一系列的帖子標題。 tldr;你的代碼工作... 你可能想檢查你發起帖子類型的位置,檢查各種設置和參數。你是在wp_head()還是get_header()之前或之後調用它? – elev

回答

-2

嘗試

the_title(); 
0

謝謝你們,

您的權利更換

get_the_title(get_the_ID()); 

它不工作,我認爲這是我嘗試的方式在Visual Composer中使用該變量。當我在下拉框中使用變量時,我可以正確看到標題,但是當我將它用於多個複選框時,它會顯示帖子ID。

這是一個Visual Composer問題。

感謝Mauro as the_title();確實簡化了代碼併爲我工作。

感謝 約翰

0

我看不到你的代碼的任何問題。嘗試使用獲取帖子並改爲使用post_title屬性。

E.g.

$my_array = array(); 

// the query 
$args=array('post_type' => 'team_members','order'=>'ASC'); 

$posts = get_posts($args); 

if ($posts) { 
    foreach ($posts as $post) { 
     $my_array[] = $post->post_title; 
    } 
} 
-1
// the query 
$args=array('post_type' => 'team_members','order'=>'ASC'); 
$my_query = new WP_Query($args); 

if ($my_query->have_posts()) : 
while ($my_query->have_posts()) : $my_query->the_post(); 
    $id = $my_query->posts->ID; 
    $my_array[]=get_the_title($id); 
endwhile; 
endif;