纯代码实现WordPress调用最新/随机/热门/指定分类文章

最省事的让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>

本文来自【年年有鱼黄金会员】的投稿,部分内容可能来源于互联网,不代表【最省事】的观点和立场。侵权投诉>>

本文著作权归博主所有,并授权【最省事】独家使用,未经博主授权,请勿转载。授权申请>>

(13)
上一篇 2019年12月21日 03:17
下一篇 2020年3月15日 00:37

相关推荐

发表回复

登录后才能评论