我想你的问题的答案就在下面的网页上。
它的作者塔里克·哈桑(Tareq Hasan)面临着与我们相同的问题,并找到了解决方案。
https://tareq.co/2013/01/sticky-posts-in-custom-post-type-archives/
基本上,你需要安装你已经拥有的插件(粘性自定义帖子类型)并添加一个过滤器:(我将代码粘贴在这里,这样如果页面关闭,我们仍然可以使用它。)
该插件非常古老,但其工作方式非常简单,因此它仍然可以与WordPress 4.2完美配合使用。这同样适用于以下代码段。
希望它能像帮助我一样帮助你。
/**
* Put sticky posts to top at custom post archives
* Author: Tareq Hasan
* Source: http://tareq.wedevs.com/2013/01/sticky-posts-in-custom-post-type-archives/
*
* WordPress doesn\'t do any processing for sticky posts in custom post type archives.
* It process sticky posts in homepage only (is_home()). This function processes
* sticky posts at custom post archive page and puts them to the top of list.
*
* @author Tareq Hasan (http://tareq.weDevs.com)
*
* @param array $posts array of queried posts
* @return array
*/
function wedevs_cpt_sticky_at_top( $posts ) {
// apply the magic on post archive only
if ( is_main_query() && is_post_type_archive() ) {
global $wp_query;
$sticky_posts = get_option( \'sticky_posts\' );
$num_posts = count( $posts );
$sticky_offset = 0;
// loop through the post array and find the sticky post
for ($i = 0; $i < $num_posts; $i++) {
// Put sticky posts at the top of the posts array
if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
$sticky_post = $posts[$i];
// Remove sticky from current position
array_splice( $posts, $i, 1 );
// Move to front, after other stickies
array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search($sticky_post->ID, $sticky_posts);
unset( $sticky_posts[$offset] );
}
}
// Fetch sticky posts that weren\'t in the query results
if ( !empty( $sticky_posts) ) {
$stickies = get_posts( array(
\'post__in\' => $sticky_posts,
\'post_type\' => $wp_query->query_vars[\'post_type\'],
\'post_status\' => \'publish\',
\'nopaging\' => true
) );
foreach ( $stickies as $sticky_post ) {
array_splice( $posts, $sticky_offset, 0, array( $sticky_post ) );
$sticky_offset++;
}
}
}
return $posts;
}
add_filter( \'the_posts\', \'wedevs_cpt_sticky_at_top\' );