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

wordpress调用全部页面 排除某个指定ID页面

要在WordPress中调用所有页面并排除指定的ID,您可以使用`WP_Query`类。以下是一个示例代码,它将查询所有页面并排除指定的页面ID:

<?php
// Page ID array to exclude
$exclude_ids = array( 6, 66, 666 ); //Replace here with the page ID you want to exclude

//Create a new WP_Query object
$args = array(
    'post_type' => 'page',
    'posts_per_page' => -1,
    'post__not_in' => $exclude_ids,
);

$custom_query = new WP_Query($args);

// 开始循环
if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
        // wodepress.com Write your loop content here, for example:
        echo '<h2>' . get_the_title() . '</h2>';
        echo '<div>' . get_the_content() . '</div>';
    endwhile;
endif;

//Reset article data
wp_reset_postdata();
?>

将上述代码添加到您的主题模板文件(如`page.php`、`index.php`等)中,或者创建一个自定义模板文件并在其中使用这段代码。这段代码将查询所有页面并排除ID为5、10和15的页面。您可以根据需要修改`$exclude_ids`数组中的ID。

请注意,如果您使用的是自定义查询,确保在使用完查询后调用`wp_reset_postdata()`函数,以便恢复全局`$post`变量。

原文

https://www.jianzhanpress.com/?p=7975


http://www.kler.cn/news/341682.html

相关文章:

  • BGP路由原理详解
  • 前端--深入了解es6语法
  • 用docker启动mysql步骤
  • 【Linux:线程锁】
  • 【SpringBoot详细教程】-09-Redis详细教程以及SpringBoot整合Redis【持续更新】
  • 【Linux-SSH远程窗口回传】使用X11或Wayland进行SSH窗口转发
  • 【Docker】Docker基本操作
  • 前端大模型入门:Langchain的不同文本分割器对比和效果展示-教你根据场景选出最合适的方式
  • 图神经网络之异构图转同构图
  • 深度学习:5种经典神经网络模型介绍
  • JavaScript 常量/数据类型/类型转换 简单学习
  • c# 可空引用类型
  • Linux服务器安全-使用非root账号登陆
  • linux udev详解
  • 深入了解 MySQL 中的 JSON_CONTAINS
  • 《深入浅出LLM基础篇》(五):Propmt工程优化
  • 光控资本:科创板股票代码开头是什么,科创板股票代码?
  • 卷积神经网络细节问题及知识点
  • LVS搭建负载均衡
  • 49.字母异位词分组