国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

wordpress調用隨機文章的一些例子

2024-09-07 00:50:33
字體:
來源:轉載
供稿:網友

隨機文章如果單條sql是非常的簡單直接使用rand就可以得到了,但是在wordpress中我們需要稍加處理即可了,下面我總結了一些方法,希望對各位有幫助.

調用隨機文章代碼:

  1. <?php 
  2. $rand_posts = get_posts(‘numberposts=10&orderby=rand’); 
  3. foreach$rand_posts as $post ) : 
  4. ?> 
  5. <!–下面是你想自定義的Loop–> 
  6. <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li> 
  7. <?php endforeach; ?> 

調用相關文章代碼,在文章頁顯示相關文章,代碼如下:

  1. <?php 
  2. $tags = wp_get_post_tags($post->ID); 
  3. if ($tags) { 
  4. $first_tag = $tags[0]->term_id; 
  5. $args=array(‘ 
  6. tag__in’ => array($first_tag), 
  7. ‘post__not_in’ => array($post->ID), 
  8. ‘showposts’=>10, 
  9. ‘caller_get_posts’=>1 
  10. ); 
  11. $my_query = new WP_Query($args); 
  12. if$my_query->have_posts() ) { 
  13. while ($my_query->have_posts()) : $my_query->the_post(); ?> 
  14. <li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title();?> <?php comments_number(‘ ‘,’(1)’,’(%)’); ?></a></li> 
  15. <?php 
  16. endwhile
  17. wp_reset_query(); 
  18. ?> 

調用同分類隨機文章,將下面代碼放到主題文章頁面single模板或者邊欄sidebar模板適當位置即可:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铁力市| 启东市| 崇礼县| 洛浦县| 大邑县| 通辽市| 孙吴县| 山阳县| 皋兰县| 昆山市| 房产| 乌什县| 常熟市| 高雄市| 敦煌市| 灌南县| 鲜城| 封开县| 武城县| 碌曲县| 阿尔山市| 黄山市| 陆川县| 梓潼县| 肥东县| 仪征市| 松江区| 屯留县| 定日县| 湛江市| 柳林县| 平湖市| 昭通市| 张家界市| 重庆市| 朝阳区| 乌什县| 罗江县| 汶上县| 平定县| 深水埗区|