wordpress显示摘要_如何在WordPress中显示最新的即时贴

wordpress显示摘要

WordPress has this very cool feature called sticky posts. 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中显示最新的即时贴。

Latest Sticky Posts Demo

Note: This is an intermediate level tutorial and requires basic HTML / CSS knowledge + WordPress themes knowledge.

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

影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

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

First thing you need to do is copy and paste this code snippet in your theme’s functions.php file or in a site-specific plugin.

首先,您需要将此代码段复制并粘贴到主题的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');

The code above queries the WordPress database to retrieve the 5 latest sticky posts. It then displays each sticky post’s title with a link in a list format. We have wrapped all that in a function and created a shortcode.

上面的代码查询WordPress数据库以检索5个最新的即时贴。 然后,它以列表格式显示每个即时贴的标题和链接。 我们将所有内容包装在一个函数中,并创建了一个简码。

Now to display your latest sticky posts, you can use the shortcode [latest_stickies] in any WordPress post, page, or even a text widget.

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

If you would like to use shortcodes inside a text widget, then you will need to add this extra line of code in your theme’s functions.php or site-specific plugin.

如果您想在文本窗口小部件内使用简码,则需要在主题的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 custom homepage or a magazine style look.

此代码段和功能可以很好地用于特色滑块或您要在网站上显示的任何其他高级功能。 此代码段主要针对具有自定义首页或杂志风格的WordPress网站。

That’s all, we hope this article helped you display latest sticky posts on your WordPress blog. You may also want to checkout our tutorial on how to add an expiration date to sticky posts in WordPress.

仅此而已,我们希望本文能帮助您在WordPress博客上显示最新的粘性帖子。 您可能还想查看我们的教程,该教程如何将过期日期添加到WordPress中的粘性帖子中。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

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

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-display-the-latest-sticky-posts-in-wordpress/

wordpress显示摘要

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值