在我的主页上我有一个帖子列表,在顶部我想只显示最近的“粘性”帖子,其次是其他帖子。有没有办法实现这个目标?
仅使用一个query_posts()
的奖励积分。
(我知道如何使用两个query_posts()
来做,但我正在寻找一个不那么费力的解决方案。)
答案 0 :(得分:1)
你试过任何插件吗?比如这个? http://wordpress.org/extend/plugins/wp-sticky/我可能更容易使用/修改它们。
答案 1 :(得分:0)
您需要稍微调整一下这段代码,但它应该让您朝着正确的方向前进。这样做是修改用于拉取帖子的SQL查询。
在你的query_posts()之前;添加以下代码:
function selectSticky( $sql ){
global $sticky_posts, $wpdb;
$sticky_posts = get_option( 'sticky_posts' );
$sticky_posts = implode( ', ', $sticky_posts );
return "IF( {$wpdb->posts}.ID IN ( $sticky_posts ), 2, 1 ) as sticky, ".$sql;
}
function whereSticky( $sql ){
global $sticky_posts, $wpdb;
$sql .= " OR {$wpdb->posts}.ID IN ( $sticky_posts )";
return $sql;
}
function orderSticky( $sql ){
$sql = "sticky DESC, ".$sql;
return $sql;
}
// just for checking sql, you can remove this once everything works
function requestSticky( $sql ){
echo $sql."<br/><br/>";
return $sql;
}
add_action( 'posts_fields', 'selectSticky' );
add_action( 'posts_where', 'whereSticky' );
add_action( 'posts_orderby', 'orderSticky' );
add_action( 'posts_request', 'requestSticky' );