wordpress非插件实现最新、热门、随机文章
WordPress自带的widget侧边栏小工具虽然可以很轻松的调用最新文章,热门文章,随机文章。但使用侧边栏将会影响我们自定义的侧边栏效果。
所以很多刚使用Wordpress博客的童鞋就都选择了最简单操作用插件,但是插件太多会大大影响网站的速度,所以我自己选择无插件办法,而且只是插入很简短的一些代码,同时也能方便自定义调整样式,何乐而不为。
下面就贴出来几种常用文章方面需求的:(小波自己博客测试没啥问题)
博客版本:3.14中文版
调整页面:sidebar.php (通常大家都选择放到主题这个页面)
🧐实现最新文章代码
1 2 3 4 5 6 7
   | <ul>     <?php $post_query = new WP_Query(‘showposts=10′);     while ($post_query->have_posts()) : $post_query->the_post();     $do_not_duplicate = $post->ID; ?>     <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>     <?php endwhile;?> </ul>
 
  | 
🤔实现热门文章代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | <ul>     <?php     $post_num = 10;      $args = array(         ‘post_password’ => ”,         ‘post_status’ => ‘publish’,          ‘post__not_in’ => array($post->ID),         ‘caller_get_posts’ => 1,          ‘orderby’ => ‘comment_count’,          ‘posts_per_page’ => $post_num     );     $query_posts = new WP_Query();     $query_posts->query($args);     while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>     <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>     <?php } wp_reset_query();?> </ul>
 
  | 
🤔实现随机文章代码
1 2 3 4 5 6 7 8 9 10 11 12
   | <ul>     <?php     global $post;     $postid = $post->ID;     $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10);     $query_posts = new WP_Query();     $query_posts->query($args);     ?>     <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>     <li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>     <?php endwhile; ?> </ul>
 
  | 

『旅行者』,帮小波关注一波公众号吧。
小波需要100位关注者才能申请红包封面设计资格,万分感谢!
关注后可微信小波,前66的童鞋可以申请专属红包封面设计。
微信
支付宝