Wordpress为自定义帖子类型创建分页

时间:2014-10-30 14:09:48

标签: php wordpress pagination

我一直试图弄清楚如何将我的自定义帖子类型分类分解为页面,但我遇到了麻烦。

如果有人能告诉我如何让这项工作变得更好,我希望每隔6个帖子让档案分成新的页面。

这是我档案的当前布局:

<?php

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * News Archive Template
 */


 global $wp_query;
  $wp_query = new WP_Query( array ('posts_per_page' => 6, 'post_type' => 'news', 'post_status' => array('publish'), 
   'tax_query' => array( 
        array(
            'taxonomy' => 'news_category',
            'field' => 'slug', 
            'terms' => array( $wp_query->query['news_category']),
            'include_children' => true,
            'operator' => 'IN'
        ),
    )));

get_header(); ?>

<div id="content-archive" >

    <?php if( have_posts() ) : ?>

        <?php while( have_posts() ) : the_post(); ?>

            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <?php if( is_single() ): ?>
                    <h1 class="entry-title post-title"><?php the_title(); ?></h1>
                <?php else: ?>
                    <h2 class="entry-title post-title"><a href="<?php the_title(); ?></a></h2>
                <?php endif; ?>

                <div class="post-meta">
                <?php       
                printf( __( '<span class="%1$s">Posted on </span>%2$s<span class="%3$s"> by </span>%4$s', 'responsive' ),
                'meta-prep meta-prep-author posted',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="timestamp updated" datetime="%3$s">%4$s</time></a>',
                         esc_url( get_permalink() ),
                         esc_attr( get_the_title() ),
                         esc_html( get_the_date('c')),
                         esc_html( get_the_date() )
                ),
                'byline',
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                         get_author_posts_url( get_the_author_meta( 'ID' ) ),
                         sprintf( esc_attr__( 'View all posts by %s', 'responsive' ), get_the_author() ),
                         esc_attr( get_the_author() )
                )
            ); 
            ?>

                </div></div><!-- end of .post-meta -->
                <!-- end of .post-entry -->
                </div><!-- end of #post-<?php the_ID(); ?> -->

        <?php
        endwhile;
    else :
        echo "No posts here";

    endif;
    ?>
</div>
<!-- end of #content-archive -->

<?php get_footer(); ?>

2 个答案:

答案 0 :(得分:0)

为什么不使用默认主题存档页面?要使自定义帖子类型特定,您需要做的就是复制archive.php并将其重命名为archive- {posttype} .php

此外 - 如果您需要在该帖子类型上显示任何特定内容(如侧边栏,菜单等等),您只需编辑该单个文件即可。

如果您的默认存档页面上有正常的工作分页 - 这将有效,您可以设置WP全局管理选项中显示的帖子数。

如果您没有实施导航,我建议使用WP-PageNavi插件。

答案 1 :(得分:0)

您需要在WP_Query()中添加“paged”参数。

为导航添加paginate_links(),

试试这个,     

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * News Archive Template
 */


 global $wp_query;
 $paged = (get_query_var('page')) ? get_query_var('page') : 1;
  $wp_query = new WP_Query( array ('posts_per_page' => 6, 'post_type' => 'news', 'post_status' => array('publish'),
    'paged' => $paged,
   'tax_query' => array( 
        array(
            'taxonomy' => 'news_category',
            'field' => 'slug', 
            'terms' => array( $wp_query->query['news_category']),
            'include_children' => true,
            'operator' => 'IN'
        ),
    )));

get_header(); ?>

<div id="content-archive" >

    <?php if( have_posts() ) : ?>

        <?php while( have_posts() ) : the_post(); ?>

            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

                <?php if( is_single() ): ?>
                    <h1 class="entry-title post-title"><?php the_title(); ?></h1>
                <?php else: ?>
                    <h2 class="entry-title post-title"><a href="<?php the_title(); ?></a></h2>
                <?php endif; ?>

                <div class="post-meta">
                <?php       
                printf( __( '<span class="%1$s">Posted on </span>%2$s<span class="%3$s"> by </span>%4$s', 'responsive' ),
                'meta-prep meta-prep-author posted',
                sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="timestamp updated" datetime="%3$s">%4$s</time></a>',
                         esc_url( get_permalink() ),
                         esc_attr( get_the_title() ),
                         esc_html( get_the_date('c')),
                         esc_html( get_the_date() )
                ),
                'byline',
                sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
                         get_author_posts_url( get_the_author_meta( 'ID' ) ),
                         sprintf( esc_attr__( 'View all posts by %s', 'responsive' ), get_the_author() ),
                         esc_attr( get_the_author() )
                )
            ); 
            ?>

                </div></div><!-- end of .post-meta -->
                <!-- end of .post-entry -->
                </div><!-- end of #post-<?php the_ID(); ?> -->

        <?php
        endwhile;
    else :
        echo "No posts here";

    endif;
    echo paginate_links();
    ?>
</div>
<!-- end of #content-archive -->

<?php get_footer(); ?>