基本上我设置了两种帖子类型,“狗”和“猫”。我有一个带有两个 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; ?>
我希望他们能够与自己的分页共存,就像这里(观看视频)一样,但这似乎只适用于普通的 WP 帖子,而不适用于自定义帖子类型。如果有人对如何实现这一目标有任何想法,那将是惊人的,因为这让我发疯!
编辑
我几乎用下面的代码实现了它,但它有问题,例如,如果我在两个分页上都转到第三页,然后单击 cat post type 分页上的第一页,它将转到它已经在的同一个 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; ?>