wordpress实现调用相同分类下随机文章代码

休闲2026-01-15 12:49:15917

wordpress作为博客系统结构比较简单,实随机这个时候我们可以通过调用相同分类下的现调随机文章来加强内容和内链。那么,用相实现同分类水机文章调用的同分代码是怎样的呢?

将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可:

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

这样就能得到随机排序(第 2 个参数)的已发布(第 3 个参数)文章中的 5 篇(第 1 个参数)。

需要特别注意的文章是,如果没有第 3 个参数,代码它可能会将草稿之类的实随机文章也显示出来。

现调
本文地址:http://zxide.com/html/543e099456.html
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

全站热门

英仙座流星雨凌晨在巴尔干地区天空中划过

新手做SEO怎样才能快速挤进首页?

2025年360站长工具改版审慢?3天通过域名迁移的官方避坑指南(附EEAT实操)

Alexa优化真的有效吗?实工具推荐与效果对比

【SEO知识】竞价账户漏斗原理分析与优化

宣城企业做SEO真的有效吗?

新网站如何在3个月内挤进百度首页?

新手做网站优化必须用推广软件吗?

友情链接