如何在WordPress中显示最新的粘贴帖子

WordPress有这个非常酷的功能,称为粘贴帖子。将粘贴帖子视为您博客的精选帖子。当您将帖子标记为粘性时,它会显示在您的新帖子上方,但仅限于您的主题允许的情况。在本教程中,我们将向您展示如何在WordPress中显示最新的粘贴帖子。

Latest Sticky Posts Demo

注意:这是一个中级教程,需要基本的HTML / CSS知识+ WordPress主题知识。

视频教程

订阅WPBeginner

如果您不喜欢视频或需要更多说明,然后继续阅读。

您需要做的第一件事就是将此代码段复制并粘贴到主题的functions.php文件或特定于站点的插件中。

    function wpb_latest_sticky() {    /* Get all sticky posts */  $sticky = get_option( "sticky_posts" );    /* Sort the stickies with the newest ones at the top */  rsort( $sticky );    /* Get the 5 newest stickies (change 5 for a different number) */  $sticky = array_slice( $sticky, 0, 5 );    /* Query sticky posts */  $the_query = new WP_Query( array( "post__in" => $sticky, "ignore_sticky_posts" => 1 ) );  // The Loop  if ( $the_query->have_posts() ) {  $return .= "<ul>";  while ( $the_query->have_posts() ) {  $the_query->the_post();  $return .= "<li><a href="" .get_permalink(). "" title=""  . get_the_title() . "">" . get_the_title() . "</a><br />" . get_the_excerpt(). "</li>";    }  $return .= "</ul>";    } else {  // no posts found  }  /* Restore original Post Data */  wp_reset_postdata();    return $return;    }  add_shortcode("latest_stickies", "wpb_latest_sticky");  

上面的代码查询WordPress数据库以检索5个最新的粘贴帖子。然后,它会以列表格式显示每个粘性帖子的标题。我们已将所有内容包装在函数中并创建了一个短代码。

现在要显示最新的粘贴帖子,您可以在任何WordPress帖子,页面甚至文本小部件中使用短代码[latest_stickies]。

如果您想在文本小部件中使用短代码,那么您需要在主题的functions.php或特定于站点的插件中添加这些额外的代码行。

  add_filter("widget_text", "do_shortcode");  

此代码段和功能可以很好地用于特色滑块或您希望在站点上显示的任何其他高级功能。这个片段主要面向具有自定义主页或杂志样式外观的WordPress网站。

这就是全部,我们希望本文能帮助您在WordPress博客上显示最新的粘贴帖子。您可能还想查看我们的教程,了解如何在WordPress中为粘贴帖子添加过期日期。

如果您喜欢这篇文章,请订阅我们的YouTube频道WordPress视频教程。您也可以在Twitter和Google+上找到我们。

WordPress有这个非常酷的功能,称为粘贴帖子。将粘贴帖子视为您博客的精选帖子。当您将帖子标记为粘性时,它会显示在您的新帖子上方,但仅限于您的主题允许的情况。在本教程中,我们将向您展示如何在WordPress中显示最新的粘贴帖子。

Latest Sticky Posts Demo

注意:这是一个中级教程,需要基本的HTML / CSS知识+ WordPress主题知识。

视频教程

订阅WPBeginner

如果您不喜欢视频或需要更多说明,然后继续阅读。

您需要做的第一件事就是将此代码段复制并粘贴到主题的functions.php文件或特定于站点的插件中。

    function wpb_latest_sticky() {    /* Get all sticky posts */  $sticky = get_option( "sticky_posts" );    /* Sort the stickies with the newest ones at the top */  rsort( $sticky );    /* Get the 5 newest stickies (change 5 for a different number) */  $sticky = array_slice( $sticky, 0, 5 );    /* Query sticky posts */  $the_query = new WP_Query( array( "post__in" => $sticky, "ignore_sticky_posts" => 1 ) );  // The Loop  if ( $the_query->have_posts() ) {  $return .= "<ul>";  while ( $the_query->have_posts() ) {  $the_query->the_post();  $return .= "<li><a href="" .get_permalink(). "" title=""  . get_the_title() . "">" . get_the_title() . "</a><br />" . get_the_excerpt(). "</li>";    }  $return .= "</ul>";    } else {  // no posts found  }  /* Restore original Post Data */  wp_reset_postdata();    return $return;    }  add_shortcode("latest_stickies", "wpb_latest_sticky");  

上面的代码查询WordPress数据库以检索5个最新的粘贴帖子。然后,它会以列表格式显示每个粘性帖子的标题。我们已将所有内容包装在函数中并创建了一个短代码。

现在要显示最新的粘贴帖子,您可以在任何WordPress帖子,页面甚至文本小部件中使用短代码[latest_stickies]。

如果您想在文本小部件中使用短代码,那么您需要在主题的functions.php或特定于站点的插件中添加这些额外的代码行。

  add_filter("widget_text", "do_shortcode");  

此代码段和功能可以很好地用于特色滑块或您希望在站点上显示的任何其他高级功能。这个片段主要面向具有自定义主页或杂志样式外观的WordPress网站。

这就是全部,我们希望本文能帮助您在WordPress博客上显示最新的粘贴帖子。您可能还想查看我们的教程,了解如何在WordPress中为粘贴帖子添加过期日期。

如果您喜欢这篇文章,请订阅我们的YouTube频道WordPress视频教程。您也可以在Twitter和Google+上找到我们。

相关推荐

发表回复

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

选择聊天工具: