WordPress文章调用代码汇总

本文提供WordPress文章调用的多种方法,包括调用最新文章、指定分类文章、整站随机文章、同分类随机文章和按评论数排序的热门文章。每种方法均附有代码示例,支持最新版WordPress,可直接使用。

WordPress基础教程
阅读时间: 38 分钟
最后更新时间:2025年11月13日

关于WP文章调用方法,包括调用最新,指定分类,随机,热文等代码,经测试,支持最新版Wordpress。

调用最新文章

<?php query_posts('showposts=6&cat=-111'); ?>  // 显示篇数和排除分类

<ul>  

<?php while (have_posts()) : the_post(); ?>  

<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  

<?php endwhile;?>  

</ul>  

2调用指定分类文章

<ul>

<?php

    $args=array(

        'cat' => 1,   // 分类ID

        'posts_per_page' => 10, // 显示篇数

    );

    query_posts($args);

    if(have_posts()) : while (have_posts()) : the_post();

?>

    <li>

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

    </li>

<?php  endwhile; endif; wp_reset_query(); ?>

</ul>

3调用整站随机文章

<?php

global $post;

$postid = $post->ID;

$args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10); // 显示篇数

$query_posts = new WP_Query();

$query_posts->query($args);

?>

<?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>

</ul>

4调用同分类随机文章

<ul>

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

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endwhile;?>

<?php wp_reset_query(); ?>

</ul>

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

<ul>

<?php

$post_num = 10; // 显示篇数

$args = array(

‘post_password’ => ”,

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

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php } wp_reset_query();?>

</ul>

这篇文章有用吗?

点击星号为它评分!

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

曾凤祥

曾凤祥

技术负责人
小兽wordpress凭借多年的wordpress企业主题制作经验,坚持以“为用户而生的wordpress主题”为宗旨,累计为10万多家客户提供品质wordpress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。小兽wordpress能坚持多年,是因为我们一直诚信。

相关文章

如何让线上业务更上一层楼

还没有WordPress网站

还没有WordPress网站

不管你从事什么行业,WordPress都会为你提供一个专业的主题模板。在WordPress市场上有成千上万的免费主题,适合很多中小企业。

查看所有模板
已经有WordPress网站

已经有WordPress网站

小兽WordPress诚邀你一起学习WordPress,愿与各方携手升级改善您的WordPress网站,一起交流网站加速,网站优化等问题。

马上交个朋友
微信联系
chat 扫码联系
模板建站
挑选模板
网站定制
免费诊断
咨询热线
咨询热线

189-0733-7671

返回顶部