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

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

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

1
2
3
4
5
6
<?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生成随机文章列表

1
2
3
4
5
6
7
<?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; ?>

  

1
2
3
4
5
6
7
8
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
the_title(); //这行去掉就不显示标题
the_excerpt(); //去掉这个就不显示摘要了
endwhile;
endif; ?>

  3、调用同分类随机文章

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 随机文章
 */
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;
}

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

1
2
3
4
5
6
<div class="right">
    <h3>随便找点看看!</h3>
    <ul>
        <?php random_posts(); ?>
    </ul>
</div><!-- 随机文章 -->
posted @   ytkah  阅读(1950)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2014-10-24 微信设备公众号不用扫描相应设备二维码也可以添加关注
网址导航 gg N / G Sitemap

部分内容来源于网络,如有版权问题请联系删除

  
点击右上角即可分享
微信分享提示