2016-01-24 62 views

回答

0
query_posts('cat=123'); 

这对于特定类别来说是诀窍。如果该类别具有名称,则将该值与该名称相等。尽管建议使用类别ID(如果稍后发生更改)。

0

你真的不应该使用SQL查询,而应该使用WP_Query对象或query_posts函数。

您的查询应该是这样的:

$posts = new WP_Query(array(
    'orderby' => rand, 
    'posts_per_page' => 3, 
    'post_status' => 'publish', 
    'cat' => 123 
)); 

如果你正使用该主页上的那么“query_posts”功能更为恰当:

query_posts(array(
    'orderby' => rand, 
    'posts_per_page' => 3, 
    'post_status' => 'publish', 
    'cat' => 123 
)); 

类别ID为“123”在这种情况下,您可以在此处使用category params中的任何一个。

0
global $wpdb; 

$posts = $wpdb->get_results('SELECT ID, post_title AS title, post_excerpt AS excerpt FROM '.$wpdb->posts.' WHERE post_type = "post" AND post_status = "publish" ORDER BY post_date DESC LIMIT 5');