WordPress不用插件实现随机文章

WordPress原生没有输出随机文章列表的函数,虽然有一些插件可以实现,但过多的插件会拖慢WordPress的访问速度,因此,我们可以自己写一个函数来实现。

在主题文件functions.php中插入以下代码:

function bzg_random_posts($limit=10, $word=32, $isdate=false) {
	$rand_posts = get_posts('posts_per_page=' . $limit .'&orderby=rand');
	foreach( $rand_posts as $post ) {
		if($isdate) $article_time = 'ID ) . '">' . get_the_time('Y-m-d', $post->ID) . '';
		echo '
  • ID) . '" rel="bookmark">' . mb_strimwidth($post->post_title, 0, $word) . '' . $article_time . '
  • '; } wp_reset_postdata(); }

    在需要输出随机文章列表的地方插入以下代码:

    
    

    函数参数说明:

    • $limit:输出文章的数量;
    • $word:文章标题的长度;
    • $isdate:是否输出文章日期,true表示输出文章日期,false表示不输出文章日期。

    原创文章,作者:,如若转载,请注明出处:https://ce.771633.xyz/1592.html

    Like (0)
    Previous 2025年3月8日
    Next 2025年3月8日

    相关推荐

    发表回复

    您的邮箱地址不会被公开。 必填项已用 * 标注