2015-01-10 67 views
0

我想在我的標題上獲得帖子。 類別中的零件部分,如下所示。Wordpress通過類別獲得文章

A Category 
Post1 
Post2 
Post3 
B Category 
Post1 
Post2 
Post3 

我不想使用大量的自定義sql查詢。

我可以只做一個查詢嗎?

+1

jQuery的?我寫查詢 – Ugurcan

回答

0

這應該做的伎倆:

<?php 
add_filter('posts_join', create_function('$a', 'global $wpdb; return $a . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";')); 
add_filter('posts_where', create_function('$a', 'global $wpdb; return $a . " AND $wpdb->term_taxonomy.taxonomy = \'category\'";')); 
add_filter('posts_orderby', create_function('$a','global $wpdb; return "$wpdb->term_taxonomy.term_id DESC";')); 
query_posts($wp_query->query_vars); 
?> 
<?php while (have_posts()) : the_post() ?> 
+0

你真棒,謝謝。但我的困惑:D我該如何使用它?你能向我解釋一下嗎? – Ugurcan

+0

只需在過濾器的正常循環之前放置過濾器即可。如果它不起作用,把它放在你的主題的function.php中。 –