Recently one of our users asked us if it was possible to add sticky posts to custom post type archives. By default, WordPress has the sticky functionality available for posts, but not for other post types. In this article we will show you how to add sticky posts in WordPress custom post type archives. Before we move forward, you would probably want to learn how to 创建自定义帖子类型 in WordPress.
最近,我们的一位用户问我们是否可以在自定义帖子类型档案中添加粘性帖子。 默认情况下,WordPress具有可用于帖子的粘性功能,但不适用于其他帖子类型。 在本文中,我们将向您展示如何在WordPress自定义帖子类型档案中添加粘性帖子。 在继续进行之前,您可能需要学习如何在WordPress中创建自定义帖子类型 。
在自定义帖子类型中添加粘性帖子 (Adding Sticky Posts in Custom Post Types)
First thing you need to do is install and activate the 粘性自定义帖子类型 plugin. After activating the plugin, go to 设置 » 阅读 and scroll down to the section Sticky Custom Post Types. Next, you need to choose the custom post types where you want Stick This option to be enabled.
您需要做的第一件事是安装并激活粘性自定义帖子类型插件。 激活插件后,转到“设置”»“阅读”,然后向下滚动至“粘性自定义帖子类型”部分。 接下来,您需要选择要启用此选项的自定义帖子类型。

现在我们在这里所做的是,我们向自定义帖子类型添加了粘性帖子功能。自定义帖子类型中的置顶帖子将像常规置顶帖子一样显示在首页上。
现在,我们在这里所做的是将粘性帖子功能添加到自定义帖子类型中。与常规卡片一样,自定义帖子类型的卡片也会显示在主页上。
问题是默认情况下 WordPress 仅在主页上显示置顶帖子。它不会在存档页面上显示置顶帖子。
问题是,默认情况下,WordPress 仅在主页上显示置顶帖子。它不会在存档页面上显示置顶帖子。
在自定义帖子类型档案中显示粘性帖子 (Displaying Sticky Posts in Custom Post Type Archives)
让我们假设您有一个电影评论的自定义帖子类型,并使用我们上面提到的插件启用了粘性帖子。现在,您希望电影评论帖子类型中的粘性帖子以不同的方式显示在非粘性常规电影评论之上。像这样:
假设您有一个“电影评论”的自定义帖子类型,并使用我们上面提到的插件启用了粘性帖子。现在,您希望电影评论帖子类型中的粘性帖子以不同的方式显示在非粘性常规电影评论之上。像这样:

To achieve this goal, first thing you need is an archive template for your custom post type like this: archive-post-type.php
. Learn how to 创建自定义帖子类型存档页面. For example, if you have a custom post type movie-reviews
then your archive page template should be archive-movie-reviews.php
. If you do not have a template, create one. Simply copy the contents of archive.php in your theme’s directory and paste them into a new file archive-your-post-type.php
.
为了实现此目标,您需要的第一件事是为您的自定义帖子类型提供一个存档模板,例如: archive-post-type.php
。 了解如何创建自定义帖子类型存档页面 。 例如,如果您有一个自定义帖子类型movie-reviews
那么您的存档页面模板应该是archive-movie-reviews.php
。 如果您没有模板,请创建一个。 只需将archive.php的内容复制到主题目录中,然后将其粘贴到新文件archive-your-post-type.php
。
The next step is to add this code in your theme’s functions.php
file:
下一步是将此代码添加到主题的functions.php
文件中:
function wpb_cpt_sticky_at_top( $posts ) {
// apply it on the archives 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;
// Find the sticky posts
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] );
}
}
// Look for more sticky posts if needed
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', 'wpb_cpt_sticky_at_top' );
// Add sticky class in article title to style sticky posts differently
function cpt_sticky_class($classes) {
if ( is_sticky() ) :
$classes[] = 'sticky';
return $classes;
endif;
return $classes;
}
add_filter('post_class', 'cpt_sticky_class');
The above code would move your sticky posts to the top, and if your theme is using post_class()
function, then it would add sticky in the post class.
上面的代码会将您的粘性帖子移到顶部,如果您的主题使用的是post_class()
函数,那么它将在post类中添加粘性。
You can style your sticky posts by using .sticky
class in your stylesheet. Example:
您可以通过在样式表中使用.sticky
类来.sticky
粘性帖子的样式。 例:
.sticky {
background-color:#ededed;
background-image:url('http://example.com/wp-content/uploads/featured.png');
background-repeat:no-repeat;
background-position:right top;
}

我们希望本文可以帮助您在自定义帖子类型存档中添加置顶帖子。如有问题和反馈,请在下面发表评论。
我们希望本文可以帮助您将置顶帖子添加到自定义帖子类型存档中。如有问题和反馈,请在下面发表评论。
Source: 塔里克·哈桑
资料来源: 塔里克·哈桑
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-sticky-posts-in-custom-post-type-archives/