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

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

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

您需要做的第一件事就是安装并激活Sticky Custom Post Types插件。激活插件后,转到 Settings»读取并向下滚动到Sticky Custom Post Types部分。接下来,您需要选择您想要的自定义帖子类型Stick此选项。

Enabling sticky posts for custom post types

现在我们在这里做的是我们在自定义帖子类型中添加了粘贴帖子功能。自定义帖子类型中的粘性帖子将像常规粘贴帖子一样显示在首页上。

问题是默认情况下WordPress只显示主页上的粘贴帖子。它不会在存档页面上显示粘性帖子。

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

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

Showing a sticky post on a custom post type archive page

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

下一步是在主题的 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");    

上面的代码会将您的粘贴帖子移到顶部,如果您的主题正在使用 post_class()函数,然后它会在post类中添加sticky。

您可以在样式表中使用。sticky class来设置粘贴帖子的样式。示例:

  .sticky {  background-color:#ededed;  background-image:url("https://example.com/wp-content/uploads/featured.png");  background-repeat:no-repeat;  background-position:right top;  }  

Styling sticky posts

我们希望本文可以帮助您在自定义帖子类型存档中添加粘性帖子。如有问题和反馈,请在下面留言。

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

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

您需要做的第一件事就是安装并激活Sticky Custom Post Types插件。激活插件后,转到 Settings»读取并向下滚动到Sticky Custom Post Types部分。接下来,您需要选择您想要的自定义帖子类型Stick此选项。

Enabling sticky posts for custom post types

现在我们在这里做的是我们在自定义帖子类型中添加了粘贴帖子功能。自定义帖子类型中的粘性帖子将像常规粘贴帖子一样显示在首页上。

问题是默认情况下WordPress只显示主页上的粘贴帖子。它不会在存档页面上显示粘性帖子。

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

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

Showing a sticky post on a custom post type archive page

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

下一步是在主题的 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");    

上面的代码会将您的粘贴帖子移到顶部,如果您的主题正在使用 post_class()函数,然后它会在post类中添加sticky。

您可以在样式表中使用。sticky class来设置粘贴帖子的样式。示例:

  .sticky {  background-color:#ededed;  background-image:url("https://example.com/wp-content/uploads/featured.png");  background-repeat:no-repeat;  background-position:right top;  }  

Styling sticky posts

我们希望本文可以帮助您在自定义帖子类型存档中添加粘性帖子。如有问题和反馈,请在下面留言。

相关推荐

发表回复

房先生
我们将24小时内回复。
2024-05-10 14:03:23
您好,有任何疑问请与我们联系!
您的工单我们已经收到,我们将会尽快跟您联系!
[QQ客服]
2781198
加我微信
[电话联系]
13585372227
[电子邮件]
chaoneo@163.com
取消

选择聊天工具: