WordPress网站如何调用最新、热门文章和指定分类文章及随机文章纯代码教程

WordPress网站如何调用最新、热门文章和指定分类文章及随机文章纯代码教程

在我们WordPress主题制作过程过用到最多的应该是调用文章的代码了,而根据主题功能需求的多样性,调用文章的需求也变得花里胡哨。

下面分享了几段WordPress调用最新、热门文章的代码实例

一:最新文章


<?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;?> 

二:指定分类文章


<?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(); ?>

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


<?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();?>

四:同分类随机文章


<?php
$cat = get_the_category();
foreach($cat as $key=>$category){
$catid = $category->term_id;}
$args = array(\'orderby\' => \'rand\',\'showposts\' => 8,\'cat\' => $catid ); // 显示文章篇数
$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 endwhile;?>
<?php wp_reset_query(); ?>
1. 全库网所有资源均来源于用户上传和网络,如有侵权请发送邮箱联系站长处理!
2. 如果你有好的资源或者原创教程,可以到审核区投稿发布,分享会有钻石奖励和额外收入!
3. 全库网所有的源码、教程等其它资源均源于用户上传发布,如有疑问,可直接联系发布作者处理
4. 如有链接无法下载、失效或广告,请联系全库网管理员核实处理!
5. 通过发布原创教学视频或优质源码资源可以免费获得全库网站内SVIP会员噢
6.全库网管理猿邮箱地址:admin@qkuser.com,我们会在收到您的邮件后三个工作日内完成处理!
7. 如遇到加密压缩包,默认解压密码为"qkuser.com",如遇到无法解压的请联系管理员!

全库网 » WordPress网站如何调用最新、热门文章和指定分类文章及随机文章纯代码教程