【问题标题】:Create pagination for custom post types in Wordpress在 Wordpress 中为自定义帖子类型创建分页
【发布时间】:2012-10-08 23:42:33
【问题描述】:

我有一个想要添加分页的活动页面。但是,由于它是自定义帖子类型,我发现它相当困难。我已经设法让我的新闻页面进行分页,但我无法为事件页面获得相同的结果。这是我的活动页面代码

<?php
get_header();
get_sidebar('left');
?>
<article class="content-main events" role="main" id="post-<?php the_ID() ?>">
<?php include 'breadcrumbs.php'; ?>
<?php query_posts(array('posts_per_page'=>'2')); ?>
<?php while (have_posts()) : the_post(); ?> 
        <div class="news-post">
            <h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
            <?php the_excerpt() ?>
        </div>

        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

<!--Pagination-->
    <?php echo paginate_links( $args ) ?>
    <?php
    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
    ) );
    ?>
</article><!-- //.content-main -->
<?php

get_footer();

如果我尝试改变这一点

<?php query_posts(array('posts_per_page'=>'2')); ?>

到这里

<?php query_posts(array('category_name'=>'events','posts_per_page'=>'2')); ?>

这也不起作用。但是,如果我完全删除该行,它会显示新闻帖子类型。我被难住了!

【问题讨论】:

  • “事件”类别属于帖子类型分类法吧?

标签: wordpress pagination custom-post-type


【解决方案1】:

自定义帖子类型的分页应该与普通帖子一样。

如果您查看默认主题 TwentyEleven,您会看到他们是如何做到的。

基本上他们使用 next_posts_link()previous_posts_link() 函数。

你可以在functions.php中查看> twentytwelve_content_nav();

干杯,

【讨论】:

    【解决方案2】:

    您需要添加全局 $paged,然后在传递给 WP_Query 的数组中添加 'paged' => $paged

    ">
    <?php 
    global $paged;
    $temp = $wp_query;
                        $wp_query = null;
    
                        $wp_query = new WP_Query( array('post_type' => 'yourcustomepost','posts_per_page' => 2, 'paged' => $paged ) ); ?>
    <?php while (have_posts()) : the_post(); ?> 
            <div class="news-post">
                <h1><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h1>
                <?php the_excerpt() ?>
            </div>
    
            <?php endwhile; ?>
            <?php wp_reset_query(); ?>
    
    <!--Pagination-->
        <?php echo paginate_links( $args ) ?>
        <?php
        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
        ) );
        ?>
    </article><!-- //.content-main -->
    <?php
    

    【讨论】:

    • 你太棒了!没有太多使用 Wordpress 的经验,所以谢谢!我有最后一个查询,它只显示第一页。第二页出现 404 错误:/
    • 将 'posts_per_page' => 2 更改为 'posts_per_page' => 10 或您想要的任何数字。确保你有正确的永久链接结构,这可能是 404 错误的问题
    • 是的,我知道如何展示更多。我目前有 4 个事件,每页 3 个。当我使用分页链接时,它会显示错误。
    【解决方案3】:

    只需在functions.php文件中替换下面的函数 注意:在后端设置->阅读->每页发布:将此值设置为一个

    function twentyeleven_content_nav( $html_id ) 
    {
        global $wpdb;
        global $wp_query;
    
        if ( $wp_query->max_num_pages > 1 ) : ?>
            <nav id="<?php echo esc_attr( $html_id ); ?>">
                <h3 class="assistive-text"><?php _e( 'Post navigation', 'twentyeleven' ); ?></h3>
                <div class="nav-previous">
                    <?php next_posts_link( __( ' Previous', 'twentyeleven' ) ); ?>
                    <?php
                        $count = $wpdb->get_var( "SELECT COUNT(*) FROM wp_posts where post_type='post' and post_content<>''" );
    
                        for($j=1;$j<=$count;$j++)
                        {
                            echo "<a href='?paged=$j'> $j < </a>";
                        }
                    ?>
                    <?php previous_posts_link( __( 'Next', 'twentyeleven' ) ); ?>
                </div>
    
            </nav><!-- #nav-above -->
        <?php endif;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      相关资源
      最近更新 更多