태그 보관물: loop

loop

페이지 매김을 사용하는 여러 WP_Query 루프 정의 루프를 만들 수

이것에 대한 몇 가지 다른 질문이 있으며 WP_Query 페이지 매김이 많은 사람들에게 큰 질문 인 것 같습니다. 그래서 기능을 만드는 방법을 정확히 좁히려 고합니다.

이 코드를 페이지 매김하여 단일 사용자 정의 루프를 만들 수 있습니다.

// http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$args = array(
    'showposts' => 2,
    'paged' => $paged
);
$wp_query->query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();

// The Post
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';

endwhile;
// http://codex.wordpress.org/Function_Reference/paginate_links#Examples
$big = 999999999;
$pag_args = array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
);
echo paginate_links($pag_args);
$wp_query = null;
$wp_query = $temp;

…하지만 자연스럽게이 루프를 복사 / 붙여 넣기하면 정확한 복제본으로 작동합니다. 즉, “2 페이지”를 클릭하면 루프 모두에 대해 2 페이지로 이동합니다.

이것을 서로 분리하여 각 페이지를 개별적으로 분리하는 방법이 있습니까?

누구나 자신의 로컬 버전을 설정하고 그것에 관심이 있다면 중복 루프가있는 전체 코드가 있습니다 : http://paste.pocoo.org/show/573108/



답변

그렇습니다. 핵심은 format두 쿼리에 대해 매개 변수를 다르게 만드는 것입니다 .

    <!-- Cats -->
    <div class="animals">
        <?
            $paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
            $paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;

            // Custom Loop with Pagination 1
            // http://codex.wordpress.org/Class_Reference/WP_Query#Usage
            $args1 = array(
                'paged'          => $paged1,
                'posts_per_page' => 2,
            );
            $query1 = new WP_Query( $args1 );

            while ( $query1->have_posts() ) : $query1->the_post();
                the_title();
                echo '<br>';
                the_category(' ');
                the_excerpt();
                echo '<hr>';
            endwhile;

            // http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
            $pag_args1 = array(
                'format'  => '?paged1=%#%',
                'current' => $paged1,
                'total'   => $query1->max_num_pages,
                'add_args' => array( 'paged2' => $paged2 )
            );
            echo paginate_links( $pag_args1 );
        ?>
    </div>

    <!-- Dogs -->
    <div class="animals">
        <?
            // Custom Loop with Pagination 2
            $args2 = array(
                'paged'          => $paged2,
                'posts_per_page' => 2,
            );
            $query2 = new WP_Query( $args2 );

            while ( $query2->have_posts() ) : $query2->the_post();
                the_title();
                echo '<br>';
                the_category(' ');
                the_excerpt();
                echo '<hr>';
            endwhile;

            $pag_args2 = array(
                'format'  => '?paged2=%#%',
                'current' => $paged2,
                'total'   => $query2->max_num_pages,
                'add_args' => array( 'paged1' => $paged1 )
            );
            echo paginate_links( $pag_args2 );
        ?>
    </div>


답변