【问题标题】:Wordpress create pagination for custom post typesWordpress 为自定义帖子类型创建分页
【发布时间】:2014-10-30 22:35:59
【问题描述】:

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

如果有人能告诉我如何使这项工作变得很棒,我希望使档案每 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(); ?>

【问题讨论】:

  • 啊!让它工作!谢谢你们的帮助。在没有$paged = (get_query_var('page')) ? get_query_var('page') : 1; 的情况下单独添加'paged' =&gt; $paged, 似乎有效

标签: php wordpress pagination


【解决方案1】:

您需要在 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(); ?>

【讨论】:

  • 我在其中添加了这些,并且 URL 更改为 example.com/cat/page/2/,但它仍然显示第 1 页的相同帖子,而第 2 页没有任何帖子
  • 抱歉@william21321,这可能是因为分页后的 WP_Query() 参数中缺少逗号 => $paged。我已经编辑了代码。再试一次,让我知道。
  • 还是一样,就像 url 改变了,但是页面/帖子没有
【解决方案2】:

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

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

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

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

【讨论】:

  • 我试过了,但它改变了页面,但帖子保持不变。
  • 尝试从归档页面上的查询中删除 posts_per_page' => 6。如果存档页面上显示的全球帖子数量不同 - WP 有时会感到困惑
猜你喜欢
  • 2015-02-05
  • 2012-10-08
  • 1970-01-01
  • 2014-05-15
  • 2011-03-20
  • 2014-05-21
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
相关资源
最近更新 更多