2014-12-12 5 views
0

주어진 $ args를 기반으로 게시물을 출력하는 정상적인 루프가 있습니다. 3 개의 게시물 뒤에 추천 카테고리의 게시물을 삽입하고 싶습니다. 나는 새로운 조합을 사용하여 새로운 WP_Query, 간단한 query_posts를 시작하려고 시도했다. 아무것도 작동하는 것 같습니다. 어떤 아이디어?루프 내부의 Wordpress 루프

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => $count, 
    'paged' => $paged, 
    'page' => $paged, 
    'cat' => $cat, 
    'ignore_sticky_posts' => 1 
); 

// create a new instance of WP_Query 
$my_query = new WP_Query($args); 

<ul> 
<?php 
$i = 0; 
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); 

if($i == 3): ?> 
    <li> //insert here one post from featured category 
    </li> 
<?php endif; ?> 

<li> 
// the normal query stuff is here 
</li> 
<?php $i++ //post counter 
endwhile; //end loop while 
endif; //end loop 
?> 
</ul> 
+0

http://wordpress.stackexchange.com/questions/130009/how-to-merge-two-queries-together – rnevius

+0

그래서 get_post를 사용하여 기대, 루프 내부의 게시물을 얻을 수있는 다른 방법() 작동하지만 정확한 ID가 필요합니까? – CiprianD

+0

the_post()를 할 때'$ post' 변수에서 무엇이든 접근하고 설정할 수 있어야합니다. 그래서 먼저 기능 쿼리를 실행하고'$ featured = $ post'와 같이 자신의 변수에 해당 아이템의 내용을 설정하십시오 -'$ i == 3' 조건문에서'$ feature-> post_title'라고 말할 수 있습니다. 'get_permalink ($ feature-> ID)'등등. 다른 루프 안에서 루프를 돌릴 필요는 없다. –

답변

0

나는 이렇게했다. 주요 쿼리를하기 전에 추천 게시물에 대한 특수 쿼리를 수행했습니다.

$args2 = array(
     'post_type' => 'post', 
     'posts_per_page' => 1, 
     'category_name' => 'TheCategoryName-Slug', 
     'ignore_sticky_posts' => 1 
); 

$my_query2 = new WP_Query($args2); 
$post_ids = array(); //create an array to store the ids of the posts 
if ($my_query2->have_posts()) : while ($my_query2->have_posts()) : $my_query2->the_post(); 
    $post_ids[] = get_the_ID(); 
endwhile; 
wp_reset_postdata(); 
endif; 

모든 ID를 갖고 나면 필요로하는 데이터를 얻을 수 있습니다. 기본적으로 wp_posts 테이블의 모든 데이터 (http://codex.wordpress.org/Database_Description#Table:_wp_posts). 게시물 작성자는 get_userdata() 함수를 사용하여 이름을 가져 오는 데 사용할 수있는 ID로 반환됩니다.

echo get_post_field('post_title', $post_ids[0]); 
echo get_post_field('post_author', $post_ids[0]); 
echo get_post_field('post_date', $post_ids[0]);