最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress随机调用显示文章的方法
时间:2022-06-25 19:19:17 编辑:袖梨 来源:一聚教程网
方法一:采用wordpress内置函数,在需要的时候直接调用以下代码:
| 代码如下 | 复制代码 |
|
foreach( $rand_posts as $post ) : ?> |
|
方法二:用query_posts生成随机文章列表。
| 代码如下 | 复制代码 |
|
query_posts('showposts=10&orderby=rand'); endwhile; else: ?> 没有可显示的文章 endif; wp_reset_query(); ?> |
|
方法三:在函数模版function.php中添加函数,然后调用。
在function.php文件中添加以下代码:
| 代码如下 | 复制代码 |
|
function random_posts($posts_num=8,$before=' 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.'' . $post_title . ''; $output .= $after; } echo $output; }//random_posts()参数有$posts_num即文章数量,$before开始标签默认 |
|
然后在需要调用随机文章的地方插入下面的代码:
| 代码如下 | 复制代码 |
随便找点看看! |
|
相关文章
- 钉钉网页版快捷入口-钉钉网页版一键登录官网 03-13
- 英语cet4成绩查询入口-cet4级成绩查询官网入口 03-13
- 老王磁力官网2026最新版-老王磁力最新网址发布页 03-13
- 哔咔漫画PicACG极速下载器-带可视化界面+多线程极速下载 03-13
- 虫虫漫画在线看入口在哪-2026最新地址与高清免费漫画在线阅读指南 03-13
- 学信网官方学籍验证通道-学信网权威学历认证通道 03-13