Php 在循环期间,如何检查页面ID是否与数组中的ID匹配?

Php 在循环期间,如何检查页面ID是否与数组中的ID匹配?,php,arrays,wordpress,loops,Php,Arrays,Wordpress,Loops,我在WordPress中编写了一个自定义查询,它循环遍历4个不同的页面ID并提取页面标题。我需要做的是检查正在查看的页面是否是这些ID之一,如果是这种情况,则不显示特定的标题。我知道我基本上需要对当前页面ID进行检查,当它循环时,ID在数组中,但是我该怎么做呢 <?php $service_args = array ( 'post_type'=> 'page', 'post__in' => array(87,106,108,110), // The page

我在WordPress中编写了一个自定义查询,它循环遍历4个不同的页面ID并提取页面标题。我需要做的是检查正在查看的页面是否是这些ID之一,如果是这种情况,则不显示特定的标题。我知道我基本上需要对当前页面ID进行检查,当它循环时,ID在数组中,但是我该怎么做呢

<?php

$service_args = array (
    'post_type'=> 'page',
    'post__in' => array(87,106,108,110), // The page ID's
    'orderby' => 'ID', 
    'order' => 'ASC'
);

$servicesquery = new WP_Query( $service_args );

if ( $servicesquery->have_posts() ) {
   while ( $servicesquery->have_posts() ) {     
   $servicesquery->the_post(); 
?>

<h4><?php echo the_title(); ?></h4>

<?php } wp_reset_postdata(); ?>

您可以使用
获取当前页面/帖子Id。查找当前页面id并将其从正在准备的数组中排除

$posts_array = array(87,106,108,110);
$current_page_id = get_the_ID();

if ( ($key = array_search($current_page_id, $posts_array)) !== false) {
    unset($posts_array[$key]);
}

$service_args = array (
    'post_type'=> 'page',
    'post__in' => $posts_array, // The page ID's array
    'orderby' => 'ID', 
    'order' => 'ASC'
);

$servicesquery = new WP_Query( $service_args );

if ( $servicesquery->have_posts() ) {
    while ( $servicesquery->have_posts() ) {
        $servicesquery->the_post();
        ?>
        <h4><?php echo the_title(); ?></h4>
        <?php
    }
    wp_reset_postdata();
?>
$posts\u array=array(87106108110);
$current_page_id=获取_id();
if($key=array\u search($current\u page\u id,$posts\u array))!==false){
未设置($posts_数组[$key]);
}
$service_args=数组(
“post_type”=>“page”,
'post\u in'=>$posts\u数组,//页面ID的数组
'orderby'=>'ID',
“订单”=>“ASC”
);
$servicesquery=新的WP\u查询($service\u args);
如果($servicesquery->have_posts()){
而($servicesquery->have_posts()){
$servicesquery->the_post();
?>

尝试在while循环之外声明页面id,如下所示:

var thisPageId = get_the_ID();

while ( $servicesquery->have_posts() ) {
    if ( $servicesquery->post->ID != thisPageId ) {
        echo the_title();
    }
}

在这篇文章的帮助下,我通过使用
array_diff
检查ID来解决我的问题:


谢谢,但这段代码会导致同一个标题被无限重复。我需要的是对照
'post\uu in'=>数组(87106108110)中的ID号进行检查
如果其中一个与当前页面ID匹配,则将其从结果中排除。@liamjay66:我已编辑了我的答案。请检查一下。现在我正在查找当前页面ID,并将其从查询本身中排除。希望这对您有所帮助
$this_post = $post->ID; // Get the current page ID 
$exclude = array($this_post); // Exclude the current page ID from loop
$include = array(87,104,106,108,110); // ID's of pages to loop through

$service_args = array (
    'post_type' => 'page',
    'post__in'  => array_diff($include, $exclude),
    'orderby'   => 'ID', 
    'order'     => 'ASC'
);