WordPress 如何不使用插件实现调用最新文章、热门文章和随机文章呢,关于 WP 文章调用方法,包括调用最新,指定分类,随机,热文等代码,经测试,支持最新版 WordPress,这里我把代码记录下来,方便自己日后查看,也给大家参考之用。
调用最新文章
- <?php query_posts('showposts=6&cat=-111'); ?0 e ; ^> // 显示篇数和排除分类
- <ul>
- <?php while (have_posts()) : the_pos| ) st(); ?>
- <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>y 0 h ] P @;</li>
- <?php endwhile;?>
- </ul>
调用指定分类文章
- <ul>
- <?php
- $args=array(
- 'cat' => 1, // 分类ID
- 'posts_per_page' => 10, // 显示篇数
- )z q Z H G !;;
- query_posts($args);
- if(have_posts(& t , C U L E1;) : while (hL w C Q n c , |ave_posts()) : the_post();
- ?>
- <li>
- <ao 6 0 H href="<?pH j @hp th] q z y c | 7e_permalink(); ?>"><?php the_title(); ?></a>
- </li>
- <?php endwhile; endif; wp_reset_query(); ?>
- </ul>
调用整站u % Z \随机文章
- <ul>
- <?php
- $args = array? 6 A % @ =0; 'numberposts' => 5, 'orderby' => 'rand', 'post_status' =&D d % & 1 W E @gt; 'publish' $ A n \ @ %1;;
- $rand_posts = get_posts( $args );
- foreach( $rand_posts as $post ) : ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_titlez t % B n `40;); ?></a&y L B { A [ % `gt;</li>
- <?phpK B e w & p c endforeach; ?>
- </ul>P } C ( ] H M [ @;
调用同分类随机文章
- <ul>
- <?php
- $cat = get_tho j + C X N / O (e_categoS % H Z 5 9 9 xry();
- foreach($cat as $key=>$category&} E | }#41;2 G T ? [23;
- $catid = $category->term_id;}
- $args = array('orderby'i e 0 N C 6 => 'rand','showposts' => 8,'cat' => $catid ); // 显示篇数
- $query_posts = new WP_Query();
- $query_posts-&gO * } 1 %t;query($args);
- while ($query_posts->h5 x s 3 r Uave_posts()) : $query_posts->the_posf l ~ & lt();?>
- <li><a href=: 8 # 4 5 ! ."<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endwhile;?>
- <?php wp_reset_query(); ?>
- </uli J 6 c 8 Q H g>
调用整站热门文章(按评论数)
- <ul>
- <?php
- $post_num = 10; // 显示篇数
- $args = array(
- ‘post_password’ => ”,
- ‘post_status’ => ‘publish’, // 只选公开的文章.
- ‘post__not_in’ => array($post->ID),//排除当前文章
- ‘caller_get_pos: ! |ts’ => 1. 5 =, // 排除置顶文章.
- ‘orderby’ => ‘comment_count’, // 依评论数排序.
- ‘o q } w 2 !posts_per_page’ => $post_num
- );
- $query_posts = nei M +w WP_Query();
- $4 L ] Q ! # ]quea c h Kry_posts-d i t>query($args);
- while( $query_poa ^ Q | $ s i .sts->have_posts() ) { $query_posts->the_post(t x { 8 9 ` 2 P =); ?>
- <li><a hreS j R F + q If="<?pe P J H s v Ghp the_pev C 8 6rmalink();{ : j 6 ?>"><?php the_title(); ?></a></li&g{ m g b / qt;
- <?php } wp_reset_query()Z J ~ ^ K f 2;?>
- </ul>