2011-04-30 14 views
0

這是我的WordPress查詢,但不是wordpress相關的問題。這表明,有meta_key作爲extra1和meta_value爲測試職位如何結合這個php語句來獲得多個變量輸入的結果?

<?php $customkey1 = extra1; ?> 
<?php $customvalue1 = test; ?> 
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1 . ''); ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php the_title(); ?> 

<?php endwhile; ?> 
<?php endif; ?> 

我的問題是我怎麼還顯示,有extra1作爲metakey和test作爲metavalue的職位,但也有extra2職位作爲metakey和test2作爲同一查詢中的元值。兩個或更多變量的組合。

+1

我沒有時間回答這個問題,只是一個評論:你爲什麼要把每行? – gd1 2011-04-30 21:08:51

+0

我很確定這是一個WordPress的特定問題,因爲query_posts是一個wordpress函數。 – 2011-04-30 21:09:27

+0

可能是因爲它是一個與Html混合的WordPress主題文件。不過,不是說你應該那樣做。 – rzetterberg 2011-04-30 21:11:17

回答

0

那麼你想結合兩個查詢的結果? Check this out您可能只需製作兩個查詢對象並將其組合即可。

喜歡的東西(注意,我沒有安裝的WordPress,也不使用WordPress但假設其API是不是騙人的。):

<?php 

// The Query 
$the_query = new WP_Query($args); 
$the_second_query = new WP_Query($args); 

// The Loop 
while ($the_query->have_posts()) : $the_query->the_post(); 
    echo '<li>'; 
    the_title(); 
    echo '</li>'; 
endwhile; 

// The Second Loop 
while ($the_second_query->have_posts()) : $the_second_query->the_post(); 
    echo '<li>'; 
    the_title(); 
    echo '</li>'; 
endwhile; 

// Reset Post Data 
wp_reset_postdata(); 
?> 

注:這是醜陋的,醜陋的,並會工作,但在大多數情況下應該被專業人士認爲是錯誤的。更優雅的是,至少我會用一個函數來解析帖子。最優雅的是,我會執行其他地方的建議,將組合查詢封裝在MySQL位中。但鑑於操作員的知識,這似乎是快速解決此問題的「最佳方法」,希望只有一次。 (重複應用這種方法會使它變成一個混亂的噩夢)

+0

即使用您想要的每個查詢創建一個新的WP_Query對象。瀏覽每個查詢並將其結果添加到包含所有您想要的帖子的單個數組中。然後輸出帖子。 – 2011-04-30 21:13:31

+1

用WP_Query對象編寫自定義查詢可能會更好,它直接在mysql中進行選擇,而不必將它們放在一起。 – rzetterberg 2011-04-30 21:14:40

+0

感謝您的回覆..你可以給我發一個你正在接受的例子嗎? – EnexoOnoma 2011-04-30 21:20:30