
wordpress显示摘要
WordPress has this very cool feature called 粘帖. Think of sticky posts as featured posts for your blog. When you mark a post as sticky, it shows up above your new posts, but only if your theme permits it. In this tutorial we will show you how to display the latest sticky posts in WordPress.
WordPress具有此非常酷的功能,称为粘贴 。 将粘性帖子视为博客的特色帖子。 当您将帖子标记为粘性时,它会显示在新帖子的上方,但前提是您的主题允许。 在本教程中,我们将向您展示如何在WordPress中显示最新的即时贴。

注意:这是一个中级教程,需要基本的 HTML / CSS 知识 + WordPress 主题知识。
注意:这是一个中级教程,需要基本的 HTML/CSS 知识 + WordPress 主题知识。
影片教学 (Video Tutorial)
如果您不喜欢该视频或需要更多说明,请继续阅读。
如果您不喜欢该视频或需要更多说明,请继续阅读。
First thing you need to do is copy and paste this code snippet in your theme’s 函数.php file or in a 特定于站点的插件.
首先,您需要将此代码段复制并粘贴到主题的函数.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 数据库以检索 5 个最新的便笺。然后,它以列表格式显示每张卡的标题和链接。我们将所有内容包装在一个函数中并创建了一个短代码。
现在,要显示最新的置顶帖子,您可以在任何 WordPress 帖子、页面甚至文本小部件中使用短代码 [latest_stickies]。
现在要显示最新的置顶帖子,您可以在任何 WordPress 帖子、页面甚至文本小部件中使用短代码 [latest_stickies]。
如果您想在文本小部件中使用短代码,那么您需要在主题的functions.php或特定于站点的插件中添加这行额外的代码。
如果您想在文本小部件中使用短代码,则需要在主题的functions.php 或特定于站点的插件中添加这行额外的代码。
add_filter('widget_text', 'do_shortcode');
This snippet and function can very well be used in featured slider, or any other advanced feature that you would like to display on your site. This snippet is mostly geared toward a WordPress site that has a 自定义主页 or a magazine style look.
此代码段和功能可以很好地用于特色滑块或您要在网站上显示的任何其他高级功能。 此代码段主要针对具有自定义主页或杂志风格的WordPress网站。
就这样,我们希望本文能帮助您在 WordPress 博客上显示最新的置顶帖子。您可能还想查看我们的教程,了解如何为 WordPress 中的置顶帖子添加过期日期。
就这样,我们希望本文能帮助您在 WordPress 博客上显示最新的置顶帖子。您可能还想查看我们的教程,了解如何为 WordPress 中的置顶帖子添加过期日期。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and 谷歌+.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和谷歌+上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-the-latest-sticky-posts-in-wordpress/
wordpress显示摘要