2014-04-01 3 views
0

WordPress에서 4 개의 서로 다른 페이지 ID를 통해 사용자 지정 쿼리를 작성하고 페이지 제목을 가져옵니다. 내가해야 할 일은 볼 수있는 페이지가 해당 ID 중 하나인지 확인하고 해당 ID가 특정 제목을 표시하지 않는지 확인하는 것입니다. 나는 기본적으로 현재 페이지 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(); ?> 

답변

2

<?php get_the_ID(); ?>을 사용하면 현재 페이지/게시물 ID를 얻을 수 있습니다. 현재 페이지 ID를 찾고 준비중인 배열에서 제외시킵니다. 이 같은 while 루프 외부의 페이지 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(); 
?> 
+0

고마워요.하지만 그 코드는 동일한 제목이 무한대로 반향됩니다. 필요한 것은''post__in '=> 배열 (87,106,108,110)'의 ID 번호를 검사하고 현재 페이지 ID와 일치하면 그 결과에서 제외시킵니다. – liamjay66

+0

@ liamjay66 : 답변을 편집했습니다. 그것을 통해 가십시오. 이제 현재 페이지 ID를 찾고 쿼리 자체에서 제외했습니다. 희망이 당신을 도울 것입니다. –

0

시도 :

var thisPageId = get_the_ID(); 

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

내가 ID의에 대해 확인 array_diff를 사용하여이 포스트의 도움으로 내 문제를 해결하기 위해 관리 : https://wordpress.stackexchange.com/questions/108697/use-post-in-and-post-not-in-together

$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' 
); 
관련 문제