如何在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个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。