前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >五个wordpress调用随机文章的方法

五个wordpress调用随机文章的方法

作者头像
ytkah
发布2019-10-24 19:49:57
1.1K0
发布2019-10-24 19:49:57
举报
文章被收录于专栏:ytkahytkah

  分享几个WordPress不用插件调用随机文章的方法,不仅增强用户粘性,而且当蜘蛛来爬你的文章的时候每次都会有变化,搜索引擎很喜欢。主要用到的是orderby rand参数,下面就随ytkah一起来看看吧

  1、最直接的用法,在需要的位置放入下面的代码。

代码语言:javascript
复制
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

  2、用query_posts生成随机文章列表

代码语言:javascript
复制
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 2));
if (have_posts()) :
	while (have_posts()) : the_post();?>
		<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
	<?php endwhile; ?>
<?php endif; ?>
代码语言:javascript
复制
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
the_title(); //这行去掉就不显示标题
the_excerpt(); //去掉这个就不显示摘要了
endwhile;
endif; ?>

  3、调用同分类随机文章

代码语言:javascript
复制
<?php
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;
}
$args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid );
$query_posts = new WP_Query();
$query_posts->query($args);
while ($query_posts->have_posts()) : $query_posts->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
<?php wp_reset_query(); ?>

  4、用wp_query函数

代码语言:javascript
复制
<?php
                    $args = array(
                        'post_type' => 'post',
                        'showposts' => 4,
                        'orderby' => 'rand',
                        'cat' => -36,//除了id为36的分类
                    );
                    $my_query = new WP_Query($args);
                    if( $my_query->have_posts() ) {
                        while ($my_query->have_posts()) : $my_query->the_post(); ?>
                            <div class="item">
                                <a href="<?php the_permalink(); ?>" class="box">
                                    <?php the_post_thumbnail( array(285,360) ); ?>
                                    <div class="text">
                                        <strong><?php the_title();?></strong>
                                    </div>
                                </a>
                            </div>
                        <?php endwhile; wp_reset_query(); } ?>

  5、主题function定义

代码语言:javascript
复制
/**
 * 随机文章
 */
function random_posts($posts_num=5,$before='<li>',$after='</li>'){
	global $wpdb;
	$sql = "SELECT ID, post_title,guid
			FROM $wpdb->posts
			WHERE post_status = 'publish' ";
	$sql .= "AND post_title != '' ";
	$sql .= "AND post_password ='' ";
	$sql .= "AND post_type = 'post' ";
	$sql .= "ORDER BY RAND() LIMIT 0 , $posts_num ";
	$randposts = $wpdb->get_results($sql);
	$output = '';
	foreach ($randposts as $randpost) {
		$post_title = stripslashes($randpost->post_title);
		$permalink = get_permalink($randpost->ID);
		$output .= $before.'<a href="'
			. $permalink . '"  rel="bookmark" title="';
		$output .= $post_title . '">' . $post_title . '</a>';
		$output .= $after;
	}
	echo $output;
}

  然后在想要显示随机文章的地方加入如下代码

代码语言:javascript
复制
<div class="right">
	<h3>随便找点看看!</h3>
	<ul>
		<?php random_posts(); ?>
	</ul>
</div><!-- 随机文章 -->
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档