【发布时间】:2014-01-12 12:00:42
【问题描述】:
在我的 WordPress 模板中,我想在我的索引页面上有自定义的帖子类型分页。 下面代码的问题是,当我单击“旧帖子”链接时,它会重定向到 /page/2 URL,结果显示 404 错误。
这是我的自定义帖子类型注册码:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'video_news',
array(
'labels' => array(
'name' => __( 'Video News' ),
'singular_name' => __( 'Video News' )
),
'public' => true,
'taxonomies' => array('category'),
'has_archive' => true,
'rewrite' => array('slug' => 'videos'),
'supports' => array( 'title', 'editor', 'comments', 'excerpt', 'custom-fields', 'thumbnail' ),
)
);
}
这是我的 WP_Query 循环代码:
<?php
global $wp_query;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array('post_type' => 'video_news', 'posts_per_page' => 5, 'paged' => $paged);
$wp_query = new WP_Query($args); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<div class="pagination">
<?php previous_posts_link( 'Newer posts »' ); ?>
<?php next_posts_link('Older »') ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'No results' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
【问题讨论】:
标签: php wordpress loops pagination custom-post-type