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

相关推荐
Rainy Blue8832 小时前
从零搭建个人动态博客
博客·wordpress
hui-梦苑3 天前
[PHP]轻量主题函数WordPress 集成 KaTeX 数学公式渲染
开发语言·php·wordpress·katex
VIP_CQCRE3 天前
用 Ace Data Cloud 把 WordPress 变成你的 AI 内容营销中枢
ai·seo·wordpress·内容营销·ace data cloud
豆豆9 天前
2026 主流 CMS 建站系统对比分析:企业、个人建站该如何选型?
wordpress·建站系统·cms建站·企业建站·建站选型·saas建站·2026建站指南
VIP_CQCRE12 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 AI 写作变成可积累的 SEO 内容资产
ai·api·seo·wordpress·acedatacloud
VIP_CQCRE18 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 SEO 内容站变成长期增长资产
ai·自动化·seo·wordpress·acedatacloud
2601_9657984722 天前
Renovate Theme Setup: Fast Contractor & Construction Site Architecture
theme·wordpress
2601_9657984724 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
2601_965798471 个月前
Boutique Business Consulting WordPress Architecture & SEO Setup
android·theme·wordpress
2601_965798471 个月前
Plastic Surgery Clinic Web Setup: Deploying Rejuvita WordPress
php·theme·wordpress