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

在Wordpress中为自定义帖子类型创建分页,wordpress,pagination,custom-post-type,Wordpress,Pagination,Custom Post Type,我有一个事件页面,我想添加分页。然而,由于它是一个自定义的帖子类型,我发现它相当困难。我已经成功地为我的新闻页面设置了分页,但是我无法为事件页面设置相同的结果。这是我的事件页面代码 <?php get_header(); get_sidebar('left'); ?> <article class="content-main events" role="main" id="post-<?php the_ID() ?>"> <?php include 'b

我有一个事件页面,我想添加分页。然而,由于它是一个自定义的帖子类型,我发现它相当困难。我已经成功地为我的新闻页面设置了分页,但是我无法为事件页面设置相同的结果。这是我的事件页面代码

<?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('category_name'=>'events','posts_per_page'=>'2')); ?>


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

自定义文章类型的分页应与普通文章的分页相同

如果你看一看默认的主题,你可以看到他们是如何做到这一点的

基本上,它们使用下一篇文章链接()和上一篇文章链接()函数

您可以在functions.php>TwentyLeven_content_nav()中看到这一点


干杯,

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

">


只需替换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;
}
函数twentyeleven\u content\u nav($html\u id)
{
全球$wpdb;
全局$wp_查询;
如果($wp\u query->max\u num\u pages>1):?>

“事件”"类别属于帖子类型分类法,对吗?你太棒了!没有太多Wordpress的经验,所以谢谢你!我有最后一个查询,它只显示第一页。第二页出现404错误:/Change'posts\u per\u page'=>2改为'posts\u per\u page'=>10或任何你想要的数字。确保你有正确的permalink结构,这可能会导致错误是404错误的问题是的,我知道如何显示更多。我目前有4个事件,每页3个。当我使用分页链接时,它会显示一个错误。
<?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
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;
}