在我的主页上,我有一个帖子列表,在顶部我只想显示最近的“粘性”帖子,然后是其余的帖子。有没有办法做到这一点?
仅使用一个的奖励积分query_posts()
。
(我知道如何使用 two 来做到这一点query_posts()
,但我正在寻找一种不那么费力的解决方案。)
在我的主页上,我有一个帖子列表,在顶部我只想显示最近的“粘性”帖子,然后是其余的帖子。有没有办法做到这一点?
仅使用一个的奖励积分query_posts()
。
(我知道如何使用 two 来做到这一点query_posts()
,但我正在寻找一种不那么费力的解决方案。)
你试过任何插件吗?比如这个?http://wordpress.org/extend/plugins/wp-sticky/我可能更容易使用/修改它们。
你需要稍微调整一下这段代码,但它应该能让你朝着正确的方向前进。这样做是修改用于拉取帖子的 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' );