加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > CMS系统 > wordpress > 正文

WordPress调用最新,随机,热门,指定分类文章

发布时间:2020-12-14 14:22:07 所属栏目:wordpress 来源:网络整理
导读:由站长 通过网络收集整理的代码片段。编程之家小编现在分享给大家,也给大家做个参考。 wordpress 如何不使用插件实现调用最新文章、热门文章和随机文章呢,关于 WP 文章调用方法,包括调用最新,指定分类,随机,热文等代码,经测试,支持

以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考

wordpress 如何不使用插件实现调用最新文章、热门文章和随机文章呢,关于 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>

调用指定分类文章

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

调用整站随机文章

<ul>

<?php

$args = array( 'numberposts' => 5,'orderby' => 'rand','post_status' => 'publish' );

$rand_posts = get_posts( $args );

foreach( $rand_posts as $post ) : ?>

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

<?php endforeach; ?>

</ul>

调用同分类随机文章

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

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

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读