2014-02-22 14 views
1

對於我的生活,我不明白爲什麼這不起作用。使用數組排除頁面

我試圖讓這個選擇列表顯示除了我想排除的一對夫婦以外的所有頁面。我只是通過標題來獲取頁面,然後從中獲取ID。

<select> 
<?php 
// Get these pages by their title 
$page1 = get_page_by_title('My First Page'); 
$page2 = get_page_by_title('My Second Page'); 

// The pages to be excluded 
$excludeThese = array(
$page1->ID . ',' . 
$page2->ID 
); 

// Args for WP_Query 
$args = array(
'post__not_in' => $excludeThese, 
'post_type' => 'page', 
'posts_per_page' => -1, 
'order' => 'asc' 
); 

$pages_query = new WP_Query($args); 
while ($pages_query->have_posts()) : $pages_query->the_post();?> 
<option value="<?php the_permalink(); ?>"><?php the_title(); ?></option> 
<?php endwhile; wp_reset_query(); ?> 
</select> 

如果我回聲$第1頁和第2頁$時,頁面的ID的顯示,所以因此$ excludeThese陣列應該使用它們(是嗎?)。

如果我硬編碼的ID到$ excludeThese陣列,而不是像這樣...

$excludeThese = array(1, 2); 

...那麼這一切工作正常。所以看起來$ excludeThese陣列工作不正常。

我很想知道我在這裏做錯了什麼。

乾杯你們都。

+0

'post__not_in'應該是'post_not_in'(POST'後'一個下劃線) – celeriko

回答

1
// The pages to be excluded 
$excludeThese = array(
    $page1->ID, 
    $page2->ID 
); 

您應該使用,而不是這樣的:

// The pages to be excluded 
$excludeThese = array(
    $page1->ID . ',' . 
    $page2->ID 
); 
+0

FFS - 不能相信我沒有對回暖。做得好,謝謝 - 它很有效(顯然),以下由rationalboss和celeriko提供的解決方案。 – user3256143

0

你並不需要的參數串聯到array()

$excludeThese = array($page1->ID, $page2->ID); 
0
// The pages to be excluded 
$excludeThese = array(
$page1->ID . ',' . // there is a concatenation of string here 
$page2->ID 
); 

因爲字符串連接,你的$excludeThese would be陣列( '1,2');`

它應該是:

// The pages to be excluded 
$excludeThese = array(
$page1->ID , 
$page2->ID 
);