基本上我设置了两种帖子类型,“狗”和“猫”。我有一个带有两个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帖子,而不是自定义帖子类型。如果有人知道如何实现这一点,那将是惊人的,因为这让我疯狂!
修改 的
我几乎用以下代码实现了它,但它有问题,例如,如果我在两个分页上转到第3页,然后在cat post类型分页上单击第一页,它将转到相同的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 :(得分:1)
成功!我试图在定义$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; ?>
。以下代码是最终循环(为了测试目的,我还更改了正在使用的帖子类型名称):
{{1}}
答案 1 :(得分: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; ?>