2013-08-17 1 views
0

나는 WordPress 홈페이지를 가지고 있습니다. 모든 카테고리의 모든 게시물을 나열합니다. 이것이 WP 기본 동작입니다. 나는 "뉴스"카테고리에 표시된 그것은 그렇게 만 게시물을 어떻게홈페이지 게시물을 한 카테고리로 제한하고 끈끈한 주문 및 페이지 설정을 유지합니다.

아래의 코드는 그물 주위에 떠있는 대중적인 코드 표시됩니다. 범주를 제한하여 작동하지만 은 끈적 게시물 (게시물 순서의 맨 위로 이동하지 않음), 및 페이지 번호의 동작을 중단합니다 (다음 페이지에서 반복합니다). 도 비효율적 인입니다. 홈 페이지 카테고리 (사이트에서 가장 인기있는 페이지)를 다시 쿼리해야하기 때문입니다.

<?php 
if (is_home()) { 
    query_posts('cat=2'); // This is the category 'News'. 
} 
?> 
<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?> 
Post codes.... 

그래서 가장 좋은 방법은 무엇입니까? 이렇게하려면 높은 수준의 필터가 적절한 방법 인 것 같습니다. 어떤 워드 프레스 전문가는 이것에 대한 답을 알고 있습니까?

감사합니다. 드류

+0

이것은 WP의 버그 인 것 같습니다. 문제의 해결책을 찾을 수 있습니다. http://wordpress.stackexchange.com/q/87472/17187 –

답변

2

가 pre_get_pots 필터 사용 : 추가 정보를 위해

<ul> 
    <?php 

    $args_sticky = array(
     'cat' => 2, 
     'post__in' => get_option('sticky_posts'); 
    ); 

    /* 
    *STICKY POSTS 
    */ 
    //Display the sticky posts next 

    $the_query = new WP_Query($args_sticky); 

    while ($the_query->have_posts()) : $the_query->the_post(); 
    ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> 
    <?php 
    endwhile; 

    wp_reset_postdata(); 

    /* 
    *NON-STICKY POSTS 
    */ 
    //Display the non-sticky posts next 

    $args = array(
     'cat' => 2, 
     'post__not_in' => get_option('sticky_posts'); 
    ); 

    $the_query = new WP_Query($args); 

    while ($the_query->have_posts()) : $the_query->the_post(); 
    ?> 
     <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> 
    <?php 
    endwhile; 

    wp_reset_postdata(); 

?> 
</ul> 

볼 당신이 거의 원래의 코드로있을 수 있습니다 생각 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

function my_before_query($query) { 

    if(!is_admin() && $query->is_main_query() && is_home()){ 
     $query->set('cat', 2); 

    } 
} 
add_action('pre_get_posts', 'my_before_query', 1); 
+0

완벽하고 정확히 원하는 것입니다! 멋지고 간단하며 효율적입니다. 감사! –

0

페이지 매기기에 대한 확신이 없지만 성능 문제도 해결되지는 않지만 몇 년 전에 끈적 게시물 문제와 관련된 몇 가지 문제를 해결했습니다.

기본적으로 끈끈한 게시물이 끈적이지 않은 게시물 위에 쌓인 두 가지 쿼리를 실행합니다. 다음은 원본 코드의 단순화 된 버전이므로이 정확한 코드를 테스트하지 않았습니다. 그러나 일반적인 원칙이 있습니다. 원하는 경우 원래 구현을 게시 할 수 있습니다 (홈페이지 위젯이었습니다). http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

0

을,하지만 난 잘 모르겠어요 원래 query_string이 자동으로 추가됩니다.

<?php 
    global $query_string; 

    if (is_home()) { 
     query_posts($query_string . '&cat=2'); 
    } 
?> 

나는 제안 된 @ Simalam 해결책의 팬이다. 그것은 더 깨끗한 템플릿 코드가 될 것이다.