我们在WordPress主题开发的时候获取文章列表很简单:
比如:
<?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <div class="excerpt"> <div class="excerpt_img"><a href="<?php the_permalink(); ?>"> <?php <img class="suoluetu" src="https://img.huinaimei.cn/images/suoluetu.gif" alt="<?php the_title(); ?>" /> <?php } ?> </a></div> <div class="excerpt_right"> <div class="excerpt_t"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div> <div class="excerpt_cont"> <?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0,160,"..."); ?> </div> <div class="excerpt_more"> <a class="more_link" href="<?php the_permalink(); ?>">阅读全文 »</a> </div> <div class="excerpt_info"> <span><?php the_tags('标签:',',',''); ?></span> </div> </div> <div class="clear"></div> </div> <?php endwhile; ?> <?php else : ?> <div class="excerpt"> <p>目前还没有文章!</p> </div> <?php endif; ?>
就能获取刚刚发布的文章列表,可是如果需要文章排序如何实现?
WordPress主题开发教程:WP-Postviews调用网站热门文章浏览点击量排名文章列表
<?php $args=array( 'cat'=>20, //调用文章分类ID 'meta_key' => 'views', //排序参考哪个自定义栏目 'orderby' => 'meta_value_num', 'posts_per_page'=>6, //调用文章数量 'order' => 'DESC' //降序 ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php the_title() ?></a></li> <?php endwhile; wp_reset_query(); ?>
<?php $args = array( 'type' => 'post', 'child_of' => 0, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'category', 'pad_counts' => false ); ?>
type
(字符)post和link 其中link在新版3.0以后已被弃用。
child_of
(整数)仅显示标注了编号的分类的子类。该参数无默认值。使用该参数时应将hide_empty参数设为false
parent
(整数)只显示某个父级分类以及下面的子分类(注:子分类只显示一个层级)。
orderby
(字符)将分类按字母顺序或独有分类编号进行排序。默认为按分类 编号排序包括ID(默认)和Name
order
(字符)为类别排序(升序或降序)。默认升序。可能的值包括asc(默认)和desc
hide_empty
(布尔值)触发显示没有文章的分类。默认值为true(隐藏空类别)。有效的值包括:1(true)和0(false)
hierarchical
(布尔值)
将子类作为内部列表项目(父列表项下)的层级关系。默认为true(显示父列表项下的子类)。有效值包括1 (true)和0(false)
exclude
(字符)除去分类列表中一个或多个分类,多个可以用逗号分开,用分类ID号表示
include
(字符)只包含指定分类ID编号的分类。多个可以用逗号分开,用分类ID号表示
number
(字符)将要返回的类别数量
pad_counts
(布尔值)通过子类中的项来计算链接或文章。有效值包括1(true)和0(false),0为默认
taxonomy
(字符)返回一个分类法,这个是wordpress3.0版本后新添加的一个参数。返回的值包括category(默认)和taxonomy(一些新定义的分类名称)