国产美女裸身网站免费观看视频,最新精品国自产拍福利,影音先锋av色噜噜影院,亚洲成av人无码影片

專(zhuān)業(yè)WORDPRESS主題設(shè)計(jì)制作

wordpress常見(jiàn)的隨機(jī)文章調(diào)用方法總結(jié)(wordpress調(diào)用指定文章)

發(fā)布于: 2022-10-28

  分享幾個(gè)WordPress不用插件調(diào)用隨機(jī)文章的方法,不僅增強(qiáng)用戶粘性,而且當(dāng)蜘蛛來(lái)爬你的文章的時(shí)候每次都會(huì)有變化,搜索引擎很喜歡。主要用到的是orderby rand參數(shù),下面就隨ytkah一起來(lái)看看吧

  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生成隨機(jī)文章列表

  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(); //這行去掉就不顯示標(biāo)題
  6. the_excerpt(); //去掉這個(gè)就不顯示摘要了
  7. endwhile;
  8. endif; ?>

  3、調(diào)用同分類(lèi)隨機(jī)文章

  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函數(shù)

  1. <?php
  2. $args = array(
  3. 'post_type' => 'post',
  4. 'showposts' => 4,
  5. 'orderby' => 'rand',
  6. 'cat' => -36,//除了id為36的分類(lèi)
  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常見(jiàn)的隨機(jī)文章調(diào)用方法總結(jié)
  1. /**
  2. * 隨機(jī)文章
  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. }

  然后在想要顯示隨機(jī)文章的地方加入如下代碼

WP技術(shù)資料 wordpress模板制作、wordpress主題開(kāi)發(fā)相關(guān)知識(shí)常見(jiàn)問(wèn)題總結(jié)
MORE
服務(wù)電話:
0533-2765967

微信 13280692153