如何在WordPress自定义帖子类型档案中添加粘性帖子

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 create custom post types in WordPress.

最近,我们的一位用户问我们是否可以在自定义帖子类型档案中添加粘性帖子。 默认情况下,WordPress具有可用于帖子的粘性功能,但不适用于其他帖子类型。 在本文中,我们将向您展示如何在WordPress自定义帖子类型档案中添加粘性帖子。 在继续进行之前,您可能需要学习如何在WordPress中创建自定义帖子类型

在自定义帖子类型中添加粘性帖子 (Adding Sticky Posts in Custom Post Types)

First thing you need to do is install and activate the Sticky Custom Post Types plugin. After activating the plugin, go to Settings » Reading 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.

您需要做的第一件事是安装并激活Sticky Custom Post Types插件。 激活插件后,转到“设置”»“阅读”,然后向下滚动至“粘性自定义帖子类型”部分。 接下来,您需要选择要启用此选项的自定义帖子类型。

Enabling sticky posts for custom post types

Now what we have done here is that we have added sticky posts feature to our custom post types. Sticky posts in custom post types will be displayed on the front page just like regular sticky posts.

现在,我们在此处所做的是,我们已将粘性帖子功能添加到自定义帖子类型中。 与常规即时贴一样,自定义帖子类型的即时贴也会显示在首页上。

The problem is that by default WordPress only shows sticky posts on the home page. It does not show sticky posts on archive pages.

问题在于,默认情况下,WordPress仅在主页上显示粘性帖子。 它不会在存档页面上显示粘性帖子。

在自定义帖子类型档案中显示粘性帖子 (Displaying Sticky Posts in Custom Post Type Archives)

Lets assume that you have a custom post type for Movie Reviews with sticky posts enabled using the plugin we have mentioned above. Now you want your sticky posts in movie reviews post types to be displayed differently and on top of non-sticky regular movie reviews. Like this:

假设您有一个“电影评论”的自定义帖子类型,并且使用我们上面提到的插件启用了粘性帖子。 现在,您希望在电影评论帖子类型中的粘性帖子以不同的方式显示在非粘性常规电影评论的顶部。 像这样:

Showing a sticky post on a custom post type archive page

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 create custom post type archive page. 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;
}

Styling sticky posts

We hope this article helped you add sticky posts in custom post type archives. For questions and feedback please leave a comment below.

我们希望本文能帮助您在自定义帖子类型档案中添加粘性帖子。 对于问题和反馈,请在下面留下评论。

Source: Tareq Hasan

资料来源: Tareq Hasan

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-sticky-posts-in-custom-post-type-archives/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值