国产探花免费观看_亚洲丰满少妇自慰呻吟_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> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥化市| 普兰店市| 吉安市| 赫章县| 双城市| 遵化市| 蕲春县| 五原县| 金平| 新建县| 普格县| 宜州市| 玛多县| 孟村| 潢川县| 淮北市| 榕江县| 大荔县| 申扎县| 武义县| 广元市| 竹山县| 潞城市| 南汇区| 乳源| 抚宁县| 旬邑县| 塔城市| 客服| 泊头市| 托克逊县| 九台市| 关岭| 北海市| 沈丘县| 青川县| 桃江县| 惠来县| 枣阳市| 霍州市| 文山县|