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

相关推荐
2601_965798471 天前
Renovate Theme Setup: Fast Contractor & Construction Site Architecture
theme·wordpress
2601_965798473 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
2601_965798475 天前
Boutique Business Consulting WordPress Architecture & SEO Setup
android·theme·wordpress
2601_965798476 天前
Plastic Surgery Clinic Web Setup: Deploying Rejuvita WordPress
php·theme·wordpress
2601_965798476 天前
Contractor Web Setup: Deploying Bathrooms & Kitchens Theme Fast
前端·web3·php·wordpress
e6zzseo13 天前
wordpress独立站seo怎么做才有效
seo·wordpress·独立站·内容优化·外链建设
Web极客码15 天前
如何让用户订阅WordPress评论?提升用户互动的有效策略
运维·服务器·wordpress
fobwebs15 天前
Wordpress Xstore 主题安装Elementor Pro 后找不到某些Elements 组件的解决方法
element·wordpress·form·xstore·pro elements·elementor pro
代龙涛16 天前
WordPress sidebar.php 侧边栏开发教程
开发语言·php·wordpress
代龙涛17 天前
WordPress header.php 与 footer.php 模板结构详解
开发语言·php·wordpress