当前位置: 首页 > 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

相关文章:

  • Junit框架缺点
  • 计算机毕业设计 ——jspssm506Springboot 的旧物置换网站
  • AI大模型-提示工程学习笔记20-多模态思维链提示
  • 计算机网络-双绞线制作
  • ZIP64扩展和普通ZIP文件有什么区别?
  • [免单统计]
  • 【Python爬虫(89)】爬虫“反水”:助力数字版权保护的逆向之旅
  • 解决uniapp二次打包的安卓APP安装到物理手机后,部分页面无法访问的问题
  • SpringBoot——生成Excel文件
  • 基于 C++ Qt 的 Fluent Design 组件库 QFluentWidgets
  • python-文件系统(1)
  • 设计模式的引入
  • C语言 第一章(3)
  • NLP学习记录十:多头注意力
  • 二叉树中的深搜(典型算法思想)—— OJ例题算法解析思路
  • Python入门13:Python闭包与装饰器
  • 渗透测试(WAF过滤information_schema库的绕过,sqllib-46关,海洋cms9版本的注入)
  • Discourse 中集成 Claude 3.7 Sonnet 模型
  • AutoMQ 可观测性实践:如何使用 OpenTelemetry 监控 Kafka 和底层流存储
  • 从“Switch-case“到“智能模式“:C#模式匹配的终极进化指南
  • 世界人形机器人运动会将在北京“双奥场馆”举行
  • 水中托举救出落水孩童后遇难,42岁退役军人高武被确认为见义勇为
  • 吴清:加强监管的同时传递监管温度,尽力帮助受影响企业应对美加征关税的冲击
  • 一网红被指涉脱衣殴打霸凌事件,沈阳警方:刑拘1人,处罚5人
  • 山东莒县农商银行去年收入、利润下降,资本充足率等指标增长
  • 六大车企一季报:比亚迪近92亿净利稳居第一,多家车企营收下滑