当前位置: 首页 > news >正文

wordpress按分类ID调用最新、推荐、随机内容

在WordPress中,可以通过自定义查询(WP_Query)来按分类ID调用最新、推荐(自定义字段或标签)、随机内容。以下是一些示例代码,帮助你实现这些功能。

1. 按分类ID调用最新内容

以下代码可以调用指定分类ID下的最新文章:

<?php
// 设置分类ID和文章数量
$category_id = 1; // 替换为你的分类ID
$posts_per_page = 5; // 显示的文章数量$args = array('post_type' => 'post', // 文章类型'posts_per_page' => $posts_per_page, // 每页显示的文章数量'orderby' => 'date', // 按日期排序'order' => 'DESC', // 降序排列(最新文章在前)'cat' => $category_id // 指定分类ID
);$query = new WP_Query($args);if ($query->have_posts()) {while ($query->have_posts()) {$query->the_post();?><div class="post-item"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><p><?php the_excerpt(); ?></p></div><?php}
} else {echo '<p>没有找到文章。</p>';
}wp_reset_postdata(); // 重置查询
?>

2. 按分类ID调用推荐内容

推荐内容可以通过自定义字段或标签实现。假设你使用自定义字段is_recommended来标记推荐文章(值为true或1):

<?php
$category_id = 1; // 替换为你的分类ID
$posts_per_page = 5; // 显示的文章数量$args = array('post_type' => 'post','posts_per_page' => $posts_per_page,'orderby' => 'date', // 按日期排序'order' => 'DESC', // 降序排列'cat' => $category_id,'meta_query' => array( // 自定义字段查询array('key' => 'is_recommended', // 自定义字段名称'value' => 'true', // 自定义字段值'compare' => '='))
);$query = new WP_Query($args);if ($query->have_posts()) {while ($query->have_posts()) {$query->the_post();?><div class="post-item"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><p><?php the_excerpt(); ?></p></div><?php}
} else {echo '<p>没有找到推荐文章。</p>';
}wp_reset_postdata(); // 重置查询
?>

调用随机内容

以下代码可以调用指定分类ID下的随机文章:

<?php
$category_id = 1; // 替换为你的分类ID
$posts_per_page = 5; // 显示的文章数量$args = array('post_type' => 'post','posts_per_page' => $posts_per_page,'orderby' => 'rand', // 随机排序'cat' => $category_id
);$query = new WP_Query($args);if ($query->have_posts()) {while ($query->have_posts()) {$query->the_post();?><div class="post-item"><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><p><?php the_excerpt(); ?></p></div><?php}
} else {echo '<p>没有找到文章。</p>';
}wp_reset_postdata(); // 重置查询
?>

 原文

http://www.dulizhan.sd.cn/jianzhan/139.html

http://www.lryc.cn/news/543437.html

相关文章:

  • excel单、双字节字符转换函数(中英文输入法符号转换)
  • 能不能用Ai来开发出一款APP?很早就想过能不能用Ai来开发出一款APP?
  • lattice hdl实现spi接口
  • 超过DeepSeek、o3,Claude发布全球首个混合推理模型,并将完成新一轮35亿美元融资...
  • AI如何通过大数据分析提升制造效率和决策智能化
  • Java和JavaScript的比较
  • 2. 在Linux 当中安装 Nginx(13步) 下载安装启动(详细说明+附加详细截图说明)
  • 大模型训练——pycharm连接实验室服务器
  • 实体机器人识别虚拟环境中障碍物
  • 修改`FSL Yocto Project Community BSP`用到的u-boot源码,使其能适配百问网(100ask)的开发板
  • Rk3568驱动开发_点亮led灯(手动挡)_5
  • 十、大数据资源平台功能架构
  • LabVIEW不规则正弦波波峰波谷检测
  • 分布式主键生成服务
  • 如何通过网管提升运维效率?
  • (python)Arrow库使时间处理变得更简单
  • 机器学习数学基础:33.分半信度
  • PyTorch 源码学习:GPU 内存管理之深入分析 CUDACachingAllocator
  • 0—QT ui界面一览
  • Jenkinsfile流水线构建教程
  • flex布局自定义一行几栏,靠左对齐===grid布局
  • 开发HarmonyOS NEXT版五子棋游戏实战
  • AI革命下的多元生态:DeepSeek、ChatGPT、XAI、文心一言与通义千问的行业渗透与场景重构
  • 大语言模型(LLM)微调技术笔记
  • 六十天前端强化训练之第一天HTML5语义化标签深度解析与博客搭建实战
  • NLP的预处理数据
  • 【DeepSeek问答】QProcess::start是异步的吗?会使UI卡顿吗?
  • 【Java项目】基于Spring Boot的体质测试数据分析及可视化设计
  • JAVA-如何理解Mysql的索引
  • VUE向外暴露文件,并通过本地接口调用获取,前端自己生成接口获取public目录里面的文件