【问题标题】:WordPress Paginate custom post type on index page索引页面上的WordPress分页自定义帖子类型
【发布时间】: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 &raquo;' ); ?>
    <?php next_posts_link('Older &raquo;') ?>
</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


    【解决方案1】:

    您使用的是静态首页吗?

    如果是这样改变

    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    

    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    

    如果您不使用静态首页,您可能需要考虑使用 pre_get_posts 挂钩。 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    将以下内容插入functions.php。如果您走这条路线,请从索引模板文件中删除您的自定义查询。

    function my_home_video_news_query( $query ) {
        if ( ! is_admin() && $query->is_main_query() ) {
            if ( $query->is_home() ) {
                $query->set( 'post_type', 'video_news' );
                $query->set( 'posts_per_page', 5 );
            }
        }
    }
    add_action( 'pre_get_posts', 'my_home_video_news_query' );
    

    【讨论】:

    • 我之前尝试过在“page”和“paged”之间切换,但没有成功。
    • 你有多少常规帖子?少于您设置中的每页数量?我已经修改了我的答案,包括 pre_get_posts 解决方案。
    【解决方案2】:

    我觉得很好。

    您是否检查了永久链接设置?

    【讨论】:

    • 我确实通过来回更改永久链接(自定义页面存档有效)来“重置”永久链接,除此之外还使用帖子名称。我应该使用任何可选设置吗?
    • 您为永久链接选择了哪种设置?
    • %post-name% 尽管我在注册新的自定义 posty 类型后确实重置了它。
    【解决方案3】:

    这个

    <div class="pagination">
        <?php previous_posts_link( 'Newer posts &raquo;' ); ?>
        <?php next_posts_link('Older &raquo;') ?>
    </div>
    

    使用这个

    <div class="pagination">
    global $wp_query;
    
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages
    ) );
    </div>
    

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 2014-05-20
      • 2016-11-07
      • 2017-02-06
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 1970-01-01
      相关资源
      最近更新 更多