您现在的位置是:首页 > cms教程 > WordPress教程WordPress教程

wordpress怎么调用随机文章

文子隐2025-03-28WordPress教程已有人查阅

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

分享几个WordPress不用插件调用随机文章的方法,不仅增强用户粘性,而且当蜘蛛来爬你的文章的时候每次都会有变化,搜索引擎很喜欢。主要用到的是orderby rand参数,下面就随ytkah一起来看看吧
1、最直接的用法,在需要的位置放入下面的代码。
<?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生成随机文章列表
<?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; ?>
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
the_title(); //这行去掉就不显示标题
the_excerpt(); //去掉这个就不显示摘要了
endwhile;
endif; ?>
3、调用同分类随机文章
<?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函数
<?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(); } ?>
更详细的用法请参考WordPress自定义查询WP_Query使用方法大全
5、主题function定义
/**
* 随机文章
*/
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;
}
然后在想要显示随机文章的地方加入如下代码
<div class="right">
<h3>随便找点看看!</h3>
<ul>
<?php random_posts(); ?>
</ul>
</div><!-- 随机文章 -->

本文标签:

很赞哦! ()

相关源码

  • (自适应)简繁双语响应式服装服饰西装工装校服定制pbootcms模板本模板基于PbootCMS内核开发,为服装服饰行业量身打造,尤其适合西装定制、工装生产、校服订制等服装类企业使用。模板采用响应式布局设计,确保在手机、平板、电脑查看源码
  • (自适应)简繁双语机械矿山矿石五金设备pbootcms源码下载本模板基于PbootCMS开发,主要面向机械五金、矿山矿石设备等行业,支持简体中文和繁体中文双语切换。采用响应式布局技术,确保在各种设备上都能获得良好的浏览体验。模板设计注重展示工业设备的专业性和技术特点,帮助企业建立可靠的线上展示平台。查看源码
  • (自适应响应式)pbootcms紫色美容整形机构企业模板下载基于PbootCMS内核开发的响应式网站模板,为医疗美容机构、整形医院等企业设计,提供完整的线上展示平台采用紫色系配色方案,整体风格专业大气。模板包含首页轮播、服务项目、专家团队、案例展示等核心模块,能够充分展示医疗美容机构的专业服务和特色优势。查看源码
  • (自适应响应式)高端家用办公家具家居桌椅pbootcms模板下载为办公家具企业设计的响应式网站模板,涵盖产品展示、案例呈现、企业介绍等核心模块。通过可视化后台可快速发布实木桌椅、系统家具、办公屏风等产品信息,帮助客户直观了解材质参数与空间搭配方案。查看源码
  • (PC+WAP)绿色资源回收新能源环保设备pbootcms源码下载基于PbootCMS系统深度开发的环保行业模板,特别适配资源回收设备、新能源技术、环境治理装备等企业的线上展示需求。集成产品库、解决方案、环保案例等专业模块,助力企业高效传递绿色价值。查看源码
  • 帝国cms7.5自适应手机端在线课程教育网站源码基于帝国CMS后台管理系统,支持在线修改支付参数、联系方式等配置信息,操作界面直观便捷。专注于在线教育视频领域,提供完整的视频课程展示、付费购买及在线支付解决方案。支持多种支付渠道,满足教育培训机构的运营需求。查看源码
分享笔记 (共有 篇笔记)
验证码: