WordPress调用最新/热门/指定分类文章
这是一条文章内容页公告,可在后台开启或关闭,此处可使用html标签...
获取数据是我们在开发一款WordPress主题时经常会用到的,随着主题功能越来越多、需求的多样性,调用文章的需求也变得多种多样,最常见是应该是调用最新文章、调用热门文章、调用指定分类文章了。
今天cinui带来一篇关于调用最新/热门/指定分类文章的WordPress教程,希望能对大家有所帮助。
指定分类文章
<?php query_posts('cat=1&showposts=5'); //cat是要调用的分类ID,showposts是需要显示的文章数量 ?>
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?php the_title(); ?></a>
<?php endwhile; wp_reset_query(); ?>
最新文章
<?php query_posts('showposts=6&cat=1'); ?> // 显示文章篇数和显示分类
<?php while (have_posts()) : the_post(); ?>
<a href="<?php the_permalink() ?>" rel="external nofollow" rel="external nofollow" ><?php the_title(); ?></a>
<?php endwhile;?>
热门文章
调用整站热门文章(按评论数排序)
<?php
$post_num = 10; // 显示篇数
$args = array(
'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(); ?>
<a href="<?php the_permalink(); ?>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><?php the_title(); ?></a>
<?php } wp_reset_query();?>

UI掌控:cinui_
关注我们,获取更多的全网素材资源有趣有料!