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

相关推荐
豆豆1 天前
2026年企业如何选择CMS网站管理系统和建站系统
cms·wordpress·建站系统·内容管理系统·网站管理系统·建站软件·建站工具
小牛itbull2 天前
告别传统主题开发!ReactPress Theme Starter —— 用 Next.js 15 构建现代化无头博客
javascript·cms·react·wordpress·nextjs·reactpress·blog-theme
TG_yunshuguoji7 天前
阿里云代理商:阿里云部署 WordPress的3 种方案
人工智能·阿里云·云计算·wordpress·ai智能体
Web极客码23 天前
加快WooCommerce性能
服务器·wordpress·网站
豆豆1 个月前
WordPress至PageAdmin CMS跨平台迁移技术指南:应对环境约束的系统化过渡方案
cms·wordpress·建站系统·内容管理系统·网站管理系统·pageadmin
Web极客码1 个月前
Akismet对WordPress防垃圾评论
服务器·搜索引擎·wordpress
尘中客1 个月前
【2026最新】如何用 WordPress 零代码搭建八字排盘/紫微斗数网站(附免费开源插件)
php·api·wordpress·建站源码·网站引流
WordPress学习笔记1 个月前
B2B独立站与B2C独立站的主题模板选择的对不对很重要
wordpress·gracetheme·wodetheme
WordPress学习笔记1 个月前
一般创业公司官网用WordPress主题模板完全足够
wordpress
lilihuigz1 个月前
WordPress 7.0 AI基础设施详解:能力API、AI客户端与MCP适配器如何重塑插件生态
人工智能·wordpress·独立站