如何在WordPress中显示随机帖子

Do you want to display random posts in WordPress? Displaying random posts gives your users a chance to discover more of your content. In this article, we will show you how to easily display random posts in WordPress.

您想在WordPress中显示随机帖子吗? 显示随机帖子使您的用户有机会发现更多内容。 在本文中,我们将向您展示如何轻松地在WordPress中显示随机帖子。

How to display random posts in WordPress
为什么以及在WordPress上何处显示随机帖子 (Why and Where to Display Random Posts in WordPress)

By default WordPress lists your blog posts in reverse chronological order (from newest to oldest). This allows users to see your latest posts first.

默认情况下,WordPress按相反的时间顺序列出您的博客文章(从最新到最旧)。 这使用户可以首先查看您的最新帖子。

However, most users will not get to see your older articles. If you have been running your site for quite some time now, then your older articles will not be prominently displayed anywhere.

但是,大多数用户看不到您的旧文章。 如果您已经运行网站已有一段时间,那么您的较旧文章将不会在任何地方突出显示。

One way to overcome this is by making internal linking a habit. Linking to your older articles in new posts will help users discover them. It will also increase your pageviews and improve your SEO score.

解决此问题的一种方法是养成内部链接的习惯。 在新文章中链接到您的旧文章将有助于用户发现它们。 它还将增加您的综合浏览量并提高您的SEO得分

Another way around that is by displaying random posts in your sidebar. This way your users will get to discover posts that they would not see otherwise.

解决该问题的另一种方法是在侧边栏中显示随机帖子。 这样,您的用户将发现他们不会看到的帖子。

Having said that, let’s see how you can easily display random posts in WordPress.

话虽如此,让我们看看如何轻松地在WordPress中显示随机帖子。

影片教学 (Video Tutorial)

演示地址

If you don’t want to watch the video tutorial, then you can continue reading the text version below:

如果您不想观看视频教程,则可以继续阅读以下文本版本:

方法1:在带有插件的WordPress中显示随机帖子 (Method 1: Display Random Posts in WordPress with a Plugin)

This method is easier and is recommended for most users.

此方法更简单,建议大多数用户使用。

First thing you need to do is install and activate the Advanced Random Posts Widget plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活Advanced Random Posts Widget插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Appearance » Widgets page. You will notice a new widget labeled ‘Random Posts’ under the list of available widget.

激活后,您需要访问外观»小部件页面。 您会在可用小部件列表下方看到一个标记为“ Random Posts”的新小部件。

You need to add this widget to a sidebar. See our guide on how to add and use widgets in WordPress.

您需要将此小部件添加到侧边栏。 请参阅有关如何在WordPress中添加和使用小部件的指南。

Now, you will be able to see the widget settings. The default options will work for most websites, you can just click on the save button.

现在,您将能够看到小部件设置。 默认选项适用于大多数网站,您只需单击“保存”按钮即可。

Random posts widget settings

Advanced Random Posts Widget is a powerful plugin with tons of customization options. You can select different post types, show excerpt, show thumbnail, and skip posts you don’t want to show, or displays posts from specific categories or tags.

Advanced Random Posts Widget是一个功能强大的插件,具有大量的自定义选项。 您可以选择不同的帖子类型,显示摘录,显示缩略图以及跳过不想显示的帖子,或者显示特定类别或标签的帖子。

For more experienced users, the plugin also allows you to add custom before and after HTML, and your own custom CSS as well.

对于更有经验的用户,该插件还允许您在HTML之前和之后添加自定义,以及您自己的自定义CSS。

Don’t forget to click on the save button to store your widget settings. You can now visit your website to see random posts widget in action.

不要忘记单击“保存”按钮来存储您的小部件设置。 现在,您可以访问您的网站以查看随机帖子小部件的运行情况。

Random posts with thumbnail and excerpt
方法2:使用代码在WordPress中显示随机帖子 (Method 2: Display Random Posts in WordPress Using Code)

This method requires you to add code to your WordPress theme files. See our guide on how to copy paste code in WordPress.

此方法要求您将代码添加到WordPress主题文件中。 请参阅我们的指南, 了解如何在WordPress中复制粘贴代码

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

您需要做的第一件事是将此代码添加到主题的functions.php文件或特定站点的插件中


function wpb_rand_posts() { 

$args = array(
	'post_type' => 'post',
	'orderby'	=> 'rand',
	'posts_per_page' => 5, 
	);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {

$string .= '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		$string .= '<li><a href="'. get_permalink() .'">'. get_the_title() .'</a></li>';
	}
	$string .= '</ul>';
	/* Restore original Post Data */
	wp_reset_postdata();
} else {

$string .= 'no posts found';
}

return $string; 
} 

add_shortcode('wpb-random-posts','wpb_rand_posts');
add_filter('widget_text', 'do_shortcode'); 

This code simply creates a function that displays 5 random posts. It then creates a shortcode so that you can easily display random posts anywhere on your site. Lastly, it enables shortcodes to be executed inside WordPress widgets so that you can use shortcode inside a text widget.

这段代码只是创建了一个显示5条随机帖子的函数。 然后,它会创建一个简码,以便您可以轻松地在网站上的任意位置显示随机帖子。 最后,它允许在WordPress小部件内执行短代码,以便您可以在文本小部件内使用短代码。

Now you can display random posts inside a WordPress post, page, or text widget using the shortcode [wpb-random-posts].

现在,您可以使用简码[wpb-random-posts]在WordPress帖子,页面或文本小部件中显示随机帖子。

Plain random posts list

That’s all, we hope this article helped you learn how to display random posts in WordPress. You may also want to see these 12 WordPress sidebar tricks to increase pageviews.

仅此而已,我们希望本文能帮助您学习如何在WordPress中显示随机帖子。 您可能还想看一下这12个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 Facebook.

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值