2014-03-31 3 views
1

)로 가장 최근 게시물을 가져 오는 중입니다. 잡지 웹 사이트의 홈페이지에있는 다중 루프의 양을 줄이려고합니다.특정 사용자 정의 필드 (Wordpress

특정 사용자 정의 필드가있는 게시물을 다르게 표시하고 싶지만 해당 사용자 정의 필드 만 사용하여 첫 번째 게시물 (가장 최근)을 원합니다. 다른 루프를 작성하여이 작업을 수행 할 수 있지만 아래 루프에 포함 시키길 원합니다. 예를 들어

여기 내 쿼리입니다하지만 if (get_post_meta($post->ID, 'featured', true)):은 그래서

$fourth_query = new WP_Query($args4); 
while($fourth_query->have_posts()) : $fourth_query->the_post(); 
if (get_post_meta($post->ID, 'featured', true)): 
    get_template_part('content-opinion', get_post_format()); 
else : 
    get_template_part('content', get_post_format()); 
endif; 
endwhile; 
wp_reset_postdata(); 
+0

메타 키의 인수와 게시물 정렬을위한 값 사용 : https://codex.wordpress.org/Template_Tags/get_posts –

+0

다른 루프를 사용해야 할 것입니다. 가장 최근의 커스텀 필드가 필요한 커스텀 필드가 많은 포스트가 있으며, 나는 하나의 루프에서 그것을 수행 할 수 있기를 희망하고 있습니다. – stemie

+1

예. 가장 최근의 포스트 필터 기능 메타 값을 하나의 루프에서 가져올 수 있습니다. –

답변

1

이 함수 get_post_meta()가 값이 아닌 부울을 리턴이 조건을 충족하는 가장 최근의 게시물을 포함하기위한 그래서, 추가 조건이 필요합니다 당신은 당신이 병합 할 수 있습니다 사용자 정의 필드가 너무 빈() 함수

$fourth_query = new WP_Query($args4); 
while($fourth_query->have_posts()) : $fourth_query->the_post(); 
if (!empty(get_post_meta($post->ID, 'featured', true))): 
    get_template_part('content-opinion', get_post_format()); 
else : 
    get_template_part('content', get_post_format()); 
endif; 
endwhile; 
wp_reset_postdata(); 
+0

특정 사용자 정의 필드가있는 가장 최근 게시물을 찾고 있는데,이 사용자 정의 필드에는 많은 게시물이 있습니다. 죄송합니다. – stemie

+0

$ args4에서 showpost => 1을 설정하십시오. – AndrePliz

+0

도움을 주셔서 감사합니다. 그러나 내가 보여주고 싶은 사용자 정의 필드를 포함하지 않는 루프의 다른 게시물이 하나의 게시물 만 반환합니다. 이 작업을 수행하려면 두 개의 루프를 사용해야합니다. – stemie

1

을 시도 존재 여부를 확인해야 할 두 쿼리

<?php 
    $generalQuery    = new WP_Query($args); // there is your main query 
    $queryWithCustomField  = new WP_Query($args); // there is your query with custom post and posts_per_page = 1 
    $queryWithCustomField->posts = $queryWithCustomField->posts + $generalQuery->posts; // merge it now 

    if ($queryWithCustomField->have_posts()) : while ($queryWithCustomField->have_posts()) : $queryWithCustomField->the_post(); 
     echo "your first post is "; 
    endwhile; 
    else: 
     echo "No post Found"; 
    endif; 
?> 
관련 문제