专业WORDPRESS主题设计制作

wordpress常见的随机文章调用方法总结(wordpress调用指定文章)

发布于: 2022-10-28

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

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

  1. <?php
  2. $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
  3. $rand_posts = get_posts( $args );
  4. foreach( $rand_posts as $post ) : ?>
  5. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  6. <?php endforeach; ?>

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

  1. <?php
  2. query_posts(array('orderby' => 'rand', 'showposts' => 2));
  3. if (have_posts()) :
  4. while (have_posts()) : the_post();?>
  5. <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
  6. <?php endwhile; ?>
  7. <?php endif; ?>

  

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

  3、调用同分类随机文章

  1. <?php
  2. $cat = get_the_category();
  3. foreach($cat as $key=>$category){
  4. $catid = $category->term_id;
  5. }
  6. $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid );
  7. $query_posts = new WP_Query();
  8. $query_posts->query($args);
  9. while ($query_posts->have_posts()) : $query_posts->the_post();
  10. ?>
  11. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  12. <?php endwhile;?>
  13. <?php wp_reset_query(); ?>

  4、用wp_query函数

  1. <?php
  2. $args = array(
  3. 'post_type' => 'post',
  4. 'showposts' => 4,
  5. 'orderby' => 'rand',
  6. 'cat' => -36,//除了id为36的分类
  7. );
  8. $my_query = new WP_Query($args);
  9. if( $my_query->have_posts() ) {
  10. while ($my_query->have_posts()) : $my_query->the_post(); ?>
  11. <div class="item">
  12. <a href="<?php the_permalink(); ?>" class="box">
  13. <?php the_post_thumbnail( array(285,360) ); ?>
  14. <div class="text">
  15. <strong><?php the_title();?></strong>
  16. </div>
  17. </a>
  18. </div>
  19. <?php endwhile; wp_reset_query(); } ?>

  

  5、主题function定义

wordpress常见的随机文章调用方法总结
  1. /**
  2. * 随机文章
  3. */
  4. function random_posts($posts_num=5,$before='<li>',$after='</li>'){
  5. global $wpdb;
  6. $sql = "SELECT ID, post_title,guid
  7. FROM $wpdb->posts
  8. WHERE post_status = 'publish' ";
  9. $sql .= "AND post_title != '' ";
  10. $sql .= "AND post_password ='' ";
  11. $sql .= "AND post_type = 'post' ";
  12. $sql .= "ORDER BY RAND() LIMIT 0 , $posts_num ";
  13. $randposts = $wpdb->get_results($sql);
  14. $output = '';
  15. foreach ($randposts as $randpost) {
  16. $post_title = stripslashes($randpost->post_title);
  17. $permalink = get_permalink($randpost->ID);
  18. $output .= $before.'<a href="'
  19. . $permalink . '" rel="bookmark" title="';
  20. $output .= $post_title . '">' . $post_title . '</a>';
  21. $output .= $after;
  22. }
  23. echo $output;
  24. }

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

  1. <div class="right">
  2. <h3>随便找点看看!</h3>
  3. <ul>
  4. <?php random_posts(); ?>
  5. </ul>
  6. </div><!-- 随机文章 -->
WP技术资料 wordpress模板制作、wordpress主题开发相关知识常见问题总结
MORE
服务电话:
0533-2765967

微信 13280692153