欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」WordPress调用最新,随机,热门,指定分类代码汇总

作者 : 自学哈 本文共1701个字,预计阅读时间需要5分钟 2022-11-26 共94人阅读
也想出现在这里? 联系我们

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

1调用最新文章

<?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>
本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」WordPress调用最新,随机,热门,指定分类代码汇总
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号