跳到主要内容

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

在自定义帖子类型中添加粘性帖子

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

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

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

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

在自定义帖子类型档案中显示粘性帖子

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

要实现此目标,您需要做的第一件事是为您的自定义帖子类型创建一个存档模板,如下所示:archive-post-type.php。了解如何创建自定义帖子类型存档页面。例如,如果您具有自定义帖子类型,movie-reviews则存档页面模板应为archive-movie-reviews.php。如果您没有模板,请创建一个。只需将主题目录中archive.php的内容复制并粘贴到新文件中archive-your-post-type.php

下一步是将此代码添加到主题functions.php文件中:

1个23456789101112131415161718岁192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263functionwpb_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( $stickiesas$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
functioncpt_sticky_class($classes) {            if( is_sticky() ) :             $classes[] = 'sticky';            return$classes;        endif;         return$classes;                }    add_filter('post_class', 'cpt_sticky_class');

上面的代码会将您的粘性帖子移到顶部,如果您的主题使用的是post_class()功能,那么它将在post类中添加粘性。

您可以通过使用.sticky样式表中的类来设置粘性帖子的样式。例:

1个23456.sticky { background-color:#ededed;background-image:url('http://example.com/wp-content/uploads/featured.png');background-repeat:no-repeat;background-position:righttop;}

回到顶部