2013-08-09 6 views
1

В принципе у меня есть два типа сообщений, «Собака» и «Кошка». У меня есть шаблон страницы с двумя циклами WP_Query, отображающими два последних сообщения из каждого типа сообщения в каждом цикле. Ниже, как я регистрация типов сообщения:Несколько WP_Query с пользовательским типом сообщения и разбиением на страницы

<?php 
    function cat_post_type() { 
    register_post_type('cat', 
     array('labels' => array(
     'name' => __('Cats', 'bonestheme'), 
     'singular_name' => __('Cat', 'bonestheme'), 
     'all_items' => __('All Cats', 'bonestheme'), 
     'add_new' => __('Add New', 'bonestheme'), 
     'add_new_item' => __('Add New Cat', 'bonestheme'), 
     'edit' => __('Edit', 'bonestheme'), 
     'edit_item' => __('Edit Cats', 'bonestheme'), 
     'new_item' => __('New Cat', 'bonestheme'), 
     'view_item' => __('View Cat', 'bonestheme'), 
     'search_items' => __('Search Cat', 'bonestheme'), 
     'not_found' => __('Nothing found in the Database.', 'bonestheme'), 
     'not_found_in_trash' => __('Nothing found in Trash', 'bonestheme'), 
     'parent_item_colon' => '' 
     ), 
     'description' => __('This is the cat post type', 'bonestheme'), 
     'public' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'show_ui' => true, 
     'query_var' => true, 
     'menu_position' => 8, 
     'rewrite' => array('slug' => 'cat', 'with_front' => false), 
     'has_archive' => 'cats', 
     'capability_type' => 'post', 
     'hierarchical' => false, 
     'supports' => array('title', 'editor', 'thumbnail') 
     ) 
    ); 
    } 
    add_action('init', 'cat_post_type'); 

    function dog_post_type() { 
    register_post_type('dog', 
     array('labels' => array(
     'name' => __('Dogs', 'bonestheme'), 
     'singular_name' => __('Dog', 'bonestheme'), 
     'all_items' => __('All Dogs', 'bonestheme'), 
     'add_new' => __('Add New', 'bonestheme'), 
     'add_new_item' => __('Add New Dog', 'bonestheme'), 
     'edit' => __('Edit', 'bonestheme'), 
     'edit_item' => __('Edit Dogs', 'bonestheme'), 
     'new_item' => __('New Dog', 'bonestheme'), 
     'view_item' => __('View Dog', 'bonestheme'), 
     'search_items' => __('Search Dog', 'bonestheme'), 
     'not_found' => __('Nothing found in the Database.', 'bonestheme'), 
     'not_found_in_trash' => __('Nothing found in Trash', 'bonestheme'), 
     'parent_item_colon' => '' 
     ), 
     'description' => __('This is the dog post type', 'bonestheme'), 
     'public' => true, 
     'publicly_queryable' => true, 
     'exclude_from_search' => false, 
     'show_ui' => true, 
     'query_var' => true, 
     'menu_position' => 8, 
     'rewrite' => array('slug' => 'dog', 'with_front' => false), 
     'has_archive' => 'dogs', 
     'capability_type' => 'post', 
     'hierarchical' => false, 
     'supports' => array('title', 'editor', 'thumbnail') 
     ) 
    ); 
    } 
    add_action('init', 'dog_post_type'); 
?> 

и ниже, как я отображая типы сообщения:

<?php 
    $cat_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'cat' 
    ) 
); 
    if($cat_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($cat_query->have_posts()) : 
     $cat_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

<?php 
    $dog_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'dog' 
    ) 
); 
    if($dog_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($dog_query->have_posts()) : 
     $dog_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

Я хочу, чтобы иметь возможность сосуществовать со своей пагинацией , очень похоже на here (watch the video), но это похоже на работу только с обычными сообщениями WP, а не с пользовательскими типами сообщений. Если кто-нибудь знает, как достичь этого, это было бы удивительно, потому что это сводит меня с ума!

EDIT

Я почти достиг этого с помощью следующего кода, но он ошибочен, к примеру, если я иду на третьей странице на обоих вёрстка, затем нажмите страницу один на кота после типа пагинацией , он будет идти к тому же URL это уже:

<?php 
    $paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1; 
    $cat_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'cat', 
     'paged' => $paged1 
    ) 
); 
    if($cat_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($cat_query->have_posts()) : 
     $cat_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged1=%#%', 
      'current' => $paged1, 
      'total' => $cat_query->max_num_pages, 
      'add_args' => array('paged2' => $paged2) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

<?php 
    $paged2 = isset($_GET['paged2']) ? (int) $_GET['paged2'] : 1; 
    $dog_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'dog', 
     'paged' => $paged2 
    ) 
); 
    if($dog_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($dog_query->have_posts()) : 
     $dog_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged2=%#%', 
      'current' => $paged2, 
      'total' => $dog_query->max_num_pages, 
      'add_args' => array('paged1' => $paged1) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

ответ

0

Так вот, как можно реализовать разбиение на страницы с пользовательскими типами почтовых

<?php 

    $paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1; 
    $cat_query = new WP_Query(
    array(
     'paged'   => $paged1, 
     'posts_per_page' => 2, 
     'post_type' => 'cat' 
    ) 
); 
    if($cat_query->have_posts()) : 
?> 

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

    ?> 
    </div> 

<?php endif; 
$pag_args1 = array(
    'format' => '?paged1=%#%', 
    'current' => $paged1, 
    'total' => $cat_query->max_num_pages, 
    'add_args' => array('paged1' => $paged1) 
); 
echo paginate_links($pag_args1); 
wp_reset_postdata(); 
wp_reset_query(); 
?> 
<?php 
    $cat_query1 = new WP_Query(
    array(
     'paged'   => $paged1, 
     'posts_per_page' => 2, 
     'post_type' => 'dog' 
    ) 
); 
    if($cat_query1->have_posts()) : 
?> 

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

    ?> 
    </div> 

<?php endif; 

$pag_args2 = array(
    'format' => '?paged1=%#%', 
    'current' => $paged1, 
    'total' => $cat_query1->max_num_pages, 
    'add_args' => array('paged1' => $paged1) 
); 
echo paginate_links($pag_args2); 
wp_reset_postdata(); 
wp_reset_query(); 
?> 

Надеется, что это работает отлично

Редактировать

<?php 
    $paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1; 
    $cat_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'cat', 
     'paged' => $paged1 
    ) 
); 
    if($cat_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($cat_query->have_posts()) : 
     $cat_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged1=%#%', 
      'current' => $paged1, 
      'total' => $cat_query->max_num_pages, 
      'add_args' => array('paged1' => $paged1) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

<?php 
    $paged2 = isset($_GET['paged2']) ? (int) $_GET['paged2'] : 1; 
    $dog_query = new WP_Query(
    array(
     'posts_per_page' => 2, 
     'post_type' => 'dog', 
     'paged' => $paged2 
    ) 
); 
    if($dog_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($dog_query->have_posts()) : 
     $dog_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged2=%#%', 
      'current' => $paged2, 
      'total' => $dog_query->max_num_pages, 
      'add_args' => array('paged2' => $paged2) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 
+0

Благодарим за это, но ознакомьтесь с обновлением редактирования исходного сообщения. – Tom

+0

@ Вы поменяли '' add_args '=> массив (' paged1 '=> $ paged1) 'ваших пользовательских postypes –

+0

Смотрите мой обновленный ответ –

0

успеха! Я пытался вызвать переменную $paged2 до того, как она была определена, и оба значения $paged1 и $paged2 должны быть определены до того, как обе петли будут запущены. Следующий код - это конечные циклы (я также изменил названия типов сообщений, которые также используются для целей тестирования):

<?php 
    $paged1 = isset($_GET['paged1']) ? (int) $_GET['paged1'] : 1; 
    $paged2 = isset($_GET['paged2']) ? (int) $_GET['paged2'] : 1; 
    $product_query = new WP_Query(
    array(
     'posts_per_page' => 1, 
     'post_type' => 'product', 
     'paged' => $paged1 
    ) 
); 
    if($product_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($product_query->have_posts()) : 
     $product_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged1=%#%', 
      'current' => $paged1, 
      'total' => $product_query->max_num_pages, 
      'add_args' => array('paged2' => $paged2) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?> 

<?php 
    $person_query = new WP_Query(
    array(
     'posts_per_page' => 1, 
     'post_type' => 'person', 
     'paged' => $paged2 
    ) 
); 
    if($person_query->have_posts()) : 
?> 

    <div> 
    <?php 
     while($person_query->have_posts()) : 
     $person_query->the_post(); 
    ?> 
     <div> 
     <a href="<?php the_permalink(); ?>"> 
      <?php the_title(); ?> 
     </a> 
     </div> 
    <?php 
     endwhile; 
     echo paginate_links(
     array(
      'format' => '?paged2=%#%', 
      'current' => $paged2, 
      'total' => $person_query->max_num_pages, 
      'add_args' => array('paged1' => $paged1) 
     ) 
    ); 
     wp_reset_postdata(); 
    ?> 
    </div> 

<?php endif; ?>