2015-01-20 2 views
0

임 사이트에서 작업하고 사용자 정의 게시 유형을 빌드합니다. 이 사용자 정의 게시물 유형 내에서 1 개의 여물 16에서 숫자를 선택할 수있는 메타 필드를 추가했습니다.이 숫자는 내 홈페이지의 특정 위치와 일치합니다. 하지만 이제 내 문제가 나타납니다. 이 특정 위치에서 이러한 게시물을 반복적으로 반복하지 않고 어떻게 쿼리 할 수 ​​있습니까? 그래서 예를 들어메타 값을 기반으로 한 wordpress 쿼리

... 여기 메타 값 1 여기 메타 값 2 메타 값 포스트와 후 여기

소식 3

요법 ..

호프 사람 조금 도와 드릴 수 있습니다.

답변

0

레이아웃 논리가 얼마나 복잡한 지에 따라 몇 가지 방법으로 해결할 수 있습니다. 좀 더 복잡한 일을해야하는 경우

$args = array(
       'post-type' => 'YOUR_POST_TYPE', 
       'orderby' => 'meta_value_num', 
       'meta_key' => 'YOUR_METAFIELD_SLUG' 
     ); 

$customposts = new WP_Query($args); 
while($customposts->have_posts()): $customposts->the_post(); 

// loop goes here 

것은, 당신이 쿼리를 반복하지 않도록 할 수 있습니다 당신은 기본적으로 그냥 순서대로 16 개 개의 게시물을 통해 루프를해야하는 경우에는 게시물을 정렬하여 쿼리에서 orderby 매개 변수를 사용할 수 있습니다 한 번 쿼리 한 다음 조건을 사용하여 메타 값을 확인합니다. 예를 들어, 위 예제의 쿼리를 사용하면 ...

// Position 1 on your page 

<div class="position-1"> 

<?php 
while($customposts->have_posts()): $customposts->the_post(); 
$meta_value = get_post_meta(get_the_ID(), 'YOUR_METAFIELD_SLUG', true); 

// Only print something here if the meta value is what we want 
if($meta_value == 1) : 
    the_title(); 
    the_content(); 
endif; 

// Reset the loop 
$customposts->rewind_posts(); ?> 

</div> 

// Position 2 on your page 

<div class="position-2"> 
<?php 
while($customposts->have_posts()): $customposts->the_post(); 
$meta_value = get_post_meta(get_the_ID(), 'YOUR_METAFIELD_SLUG', true); 
if($meta_value == 2) : 
    the_title(); 
    the_content(); 
endif; 

// Reset the loop again 
$customposts->rewind_posts(); ?> 

.... 등등.

이러한 도움을 찾을 수 있습니다 :이 빠른 답변

http://codex.wordpress.org/Function_Reference/get_post_meta

http://codex.wordpress.org/Class_Reference/WP_Query

+0

감사합니다. 확실히 두 번째 예제를 사용하고 템플릿 부분에 제목과 내용을 사용하여 약간의 DRY 작업을 수행 할 것입니다. 다시 한 번 감사합니다 – Toasty

+0

post-type은 post_type이어야합니다. 아직도 고마워. – Toasty