WordPress如何调用最新/热门/指定分类文章

获取数据是我们在开发一款WordPress主题时经常会用到的,随着主题功能越来越多、需求的多样性,调用文章的需求也变得多种多样,最常见是应该是调用最新文章、调用热门文章、调用指定分类文章了。

今天悟空搜带来一篇关于调用最新/热门/指定分类文章的WordPress教程,希望能对大家有所帮助。

指定分类文章


1
2
3
4
5
6
<?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"  ><?php the_title(); ?></a>

<?php endwhile; wp_reset_query(); ?>

最新文章


1
2
3
4
5
6
<?php query_posts('showposts=6&cat=1'); ?> // 显示文章篇数和显示分类
<?php while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink() ?>" rel="external nofollow" ><?php the_title(); ?></a>

<?php endwhile;?>

热门文章

调用整站热门文章(按评论数排序)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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"   ><?php the_title(); ?></a>

<?php } wp_reset_query();?>

原文链接:https://xiaohost.com/1195.html,转载请注明出处。
0

评论0

请先