2014-09-04 51 views
0

我在使用query_posts的wordpress中遇到問題。如何使用類別ID獲取wordpress文章?

這裏是我的代碼:

$args = array('category__in' => array(9),'post_type'=>'banner-slider','post_status'=>'publish'); 
query_posts($args); 
while (have_posts()) : the_post(); 
the_title(); 
endwhile; 

query_post回報以下查詢:

SELECT SQL_CALC_FOUND_ROWS bib_posts.* FROM bib_posts WHERE 1=1 AND 0 = 1 AND bib_posts.post_type = 'banner-slider' AND ((bib_posts.post_status = 'publish')) GROUP BY bib_posts.ID ORDER BY bib_posts.post_date DESC LIMIT 0, 10 

在上面的查詢,我得到0 = 1,這是錯誤的。但是當我從查詢文章中刪除category__in然後我的查詢工作正常。

請告訴我我錯在哪裏。

回答

1

query_posts並不意味着通過插件或主題中使用,更具體的說法典:

此功能並不意味着通過插件或主題中使用。正如後面解釋的 ,有更好,更高性能的選項來更改主要查詢的 。 query_posts()過於簡單和有問題的方式 修改頁面的主要查詢,將其替換爲查詢的新實例 。這是效率低下的(重新運行SQL查詢),並會在某些情況下(特別是在處理帖子 分頁時)失敗 。任何現代WP代碼都應該使用更可靠的方法,例如 利用pre_get_posts鉤子來實現此目的。建議使用WP_Queryget_posts()http://codex.wordpress.org/Function_Reference/query_posts

query_posts

WordPress的抄本:

引自。在你的情況下,我認爲get_posts()應該是足夠的,所以這個例子將使用它。下面的例子實際上是從WordPress抄本,與一對夫婦修改爲您categorypost_typepost_status

<?php 
$args = array('category' => 9, 'post_type' => 'banner-slider', 'post_status' => 'publish'); 

$myposts = get_posts($args); 
foreach ($myposts as $post) : setup_postdata($post); ?> 
    <li> 
     <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
    </li> 
<?php 
endforeach; 
wp_reset_postdata(); 
?> 

食品:http://codex.wordpress.org/Template_Tags/get_posts

相關問題