优选主流主机商
任何主机均需规范使用

WordPress免插件实现最新、热门、随机文章调用详细代码教程

调用最新文章:
 

  1. <ul>
  2. <?php $post_query = new WP_Query(‘showposts=10′);
  3. while ($post_query->have_posts()) : $post_query->the_post();
  4. $do_not_duplicate = $post->ID; ?>
  5. <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
  6. <?php endwhile;?>
  7. </ul>

调用热门文章: 

  1. <ul>
  2. <?php
  3. $post_num = 10; // 设置调用条数
  4. $args = array(
  5. ‘post_password’ => ”,
  6. ‘post_status’ => ‘publish’, // 只选公开的文章.
  7. ‘post__not_in’ => array($post->ID),//排除当前文章
  8. ‘caller_get_posts’ => 1, // 排除置顶文章.
  9. ‘orderby’ => ‘comment_count’, // 依评论数排序.
  10. ‘posts_per_page’ => $post_num
  11. );
  12. $query_posts = new WP_Query();
  13. $query_posts->query($args);
  14. while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
  15. <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
  16. <?php } wp_reset_query();?>
  17. </ul>

调用随机文章:

  1. <ul>
  2. <?php
  3. global $post;
  4. $postid = $post->ID;
  5. $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10);
  6. $query_posts = new WP_Query();
  7. $query_posts->query($args);
  8. ?>
  9. <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
  10. <li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
  11. <?php endwhile; ?>
  12. </ul>
未经允许不得转载:搬瓦工中文网 » WordPress免插件实现最新、热门、随机文章调用详细代码教程