数字分页自定义帖子类型

时间:2016-12-31 作者:skifast

我创建了一个自定义页面来显示带有自定义字段的cpt循环。

我需要添加数字分页,我尝试使用此代码,但没有成功。

Functions.php

function pagination_bar() {
    global $wp_query;

    $total_pages = $wp_query->max_num_pages;

    if ($total_pages > 1){
        $current_page = max(1, get_query_var(\'paged\'));

        echo paginate_links(array(
            \'base\' => get_pagenum_link(1) . \'%_%\',
            \'format\' => \'/page/%#%\',
            \'current\' => $current_page,
            \'total\' => $total_pages,
        ));
    }
}

custompage.php

<!--Loop Salmi-->
<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$loop = new WP_Query( array( \'post_type\' => \'salmi\',
        \'posts_per_page\' => 15,
        \'paged\'          => $paged )
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <!--Colonne Contenuto -->
    <div class="salmicpt">
        <div class="wpb_column vc_column_container td-pb-span8">
            <div class="titlecpt"><?php the_title(); ?></div>
        </div>
        <div class="wpb_column vc_column_container td-pb-span4">
            <?php if( get_field(\'audio_salmi\') ): ?>
                <a href="<?php the_field(\'audio_salmi\'); ?>" ><img src="mysite.com/wp-content/uploads/cuffia-cpt-e1481533293805.png" alt="Ascolta" title="Ascolta" /></a>
            <?php endif; ?>
            <?php if( get_field(\'salmi_pdf\') ): ?>
                <a href="<?php the_field(\'salmi_pdf\'); ?>" ><img src="mysite.com/wp-content/uploads/freccia-32.png" alt="Scarica il PDf" title="Scarica il PDF" /></a>
            <?php endif; ?>
        </div>
        <div style=\'clear:both\'></div><hr class="style-one" />
    </div>
    <nav class="pagination">
        <?php pagination_bar(); ?>
    </nav>

<?php endwhile; wp_reset_query(); ?> 
哪里错了??谢谢

3 个回复
最合适的回答,由SO网友:Tunji 整理而成

您正在引用global $wp_query 使用重置的函数中的对象wp_reset_query().

您可以通过传递自定义$loop WP\\u查询对象到函数。我也改变了wp_reset_querywp_reset_postdata

此外,您正在调用中的分页函数while loop 而不是在它之后。

Your function should be updated to:

function pagination_bar( $custom_query ) {

    $total_pages = $custom_query->max_num_pages;
    $big = 999999999; // need an unlikely integer

    if ($total_pages > 1){
        $current_page = max(1, get_query_var(\'paged\'));

        echo paginate_links(array(
            \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\' => \'?paged=%#%\',
            \'current\' => $current_page,
            \'total\' => $total_pages,
        ));
    }
}
在你的custompage.php 文件:

<!--Loop Salmi-->
<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$loop = new WP_Query( array( \'post_type\' => \'salmi\',
        \'posts_per_page\' => 15,
        \'paged\'          => $paged )
);
if ( $loop->have_posts() ):
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <!--Colonne Contenuto -->
    <div class="salmicpt">
        <div class="wpb_column vc_column_container td-pb-span8">
            <div class="titlecpt"><?php the_title(); ?></div>
        </div>
        <div class="wpb_column vc_column_container td-pb-span4">
            <?php if( get_field(\'audio_salmi\') ): ?>
                <a href="<?php the_field(\'audio_salmi\'); ?>" ><img src="mysite.com/wp-content/uploads/cuffia-cpt-e1481533293805.png" alt="Ascolta" title="Ascolta" /></a>
            <?php endif; ?>
            <?php if( get_field(\'salmi_pdf\') ): ?>
                <a href="<?php the_field(\'salmi_pdf\'); ?>" ><img src="mysite.com/wp-content/uploads/freccia-32.png" alt="Scarica il PDf" title="Scarica il PDF" /></a>
            <?php endif; ?>
        </div>
        <div style=\'clear:both\'></div><hr class="style-one" />
    </div>
    <?php endwhile; ?>
    <nav class="pagination">
        <?php pagination_bar( $loop ); ?>
    </nav>
<?php wp_reset_postdata();
endif;

SO网友:NikHiL Gadhiya

copy and pest in your function file

function pagination_bar( $query_wp ) 
{
    $pages = $query_wp->max_num_pages;
    $big = 999999999; // need an unlikely integer
    if ($pages > 1)
    {
        $page_current = max(1, get_query_var(\'paged\'));
        echo paginate_links(array(
            \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
            \'format\' => \'?paged=%#%\',
            \'current\' => $page_current,
            \'total\' => $pages,
        ));
    }
}
**在您的custompage中复制有害生物代码。php文件**

<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$args = array(\'post_type\'=>\'salmi\',\'posts_per_page\' => 15,\'paged\' => $paged);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>    
    <!--Colonne Contenuto -->
    <div class="salmicpt">
        <div class="wpb_column vc_column_container td-pb-span8">
            <div class="titlecpt"><?php the_title(); ?></div>
        </div>
        <div class="wpb_column vc_column_container td-pb-span4">
            <?php if( get_field(\'audio_salmi\') ): ?>
                <a href="<?php the_field(\'audio_salmi\'); ?>" >
                    <img src="mysite.com/wp-content/uploads/cuffia-cpt-e1481533293805.png" alt="Ascolta" title="Ascolta" />
                </a>
            <?php endif; ?>
            <?php if( get_field(\'salmi_pdf\') ): ?>
                <a href="<?php the_field(\'salmi_pdf\'); ?>" >
                    <img src="mysite.com/wp-content/uploads/freccia-32.png" alt="Scarica il PDf" title="Scarica il PDF" />
                </a>
            <?php endif; ?>
        </div>
        <div style=\'clear:both\'></div><hr class="style-one" />
    </div>
    <?php endwhile; ?>
    <nav class="pagination">
        <?php pagination_bar( $the_query ); ?>
    </nav>
<?php wp_reset_postdata();
endif;

SO网友:kaustubh chaudhari

Make following changes in custompage.php file

<?php
$paged = ( get_query_var( \'page\' ) ) ? get_query_var( \'page\' ) : 1;
$args = array(\'post_type\'=>\'salmi\',\'posts_per_page\' => 15,\'paged\' => $paged);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ):
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>    
    <!--Colonne Contenuto -->
    <div class="salmicpt">
        <div class="wpb_column vc_column_container td-pb-span8">
            <div class="titlecpt"><?php the_title(); ?></div>
        </div>
        <div class="wpb_column vc_column_container td-pb-span4">
            <?php if( get_field(\'audio_salmi\') ): ?>
                <a href="<?php the_field(\'audio_salmi\'); ?>" >
                    <img src="mysite.com/wp-content/uploads/cuffia-cpt-e1481533293805.png" alt="Ascolta" title="Ascolta" />
                </a>
            <?php endif; ?>
            <?php if( get_field(\'salmi_pdf\') ): ?>
                <a href="<?php the_field(\'salmi_pdf\'); ?>" >
                    <img src="mysite.com/wp-content/uploads/freccia-32.png" alt="Scarica il PDf" title="Scarica il PDF" />
                </a>
            <?php endif; ?>
        </div>
        <div style=\'clear:both\'></div><hr class="style-one" />
    </div>
    <?php endwhile; ?>
    <nav class="pagination">
        <?php pagination_bar( $the_query ); ?>
    </nav>
<?php wp_reset_postdata();
endif;
</div>

And in functions.php

function pagination_bar( $query_wp ) 
{
$pages = $query_wp->max_num_pages;
$big = 999999999; // need an unlikely integer
if ($pages > 1)
{
    $page_current = max(1, get_query_var(\'page\'));
    echo paginate_links(array(
        \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\' => \'?paged=%#%\',
        \'current\' => $page_current,
        \'total\' => $pages,
    ));
}
}

This works perfectly

相关推荐

Can't Add Pagination

我无法添加分页来在页面之间划分帖子并在页面之间导航。目前所有20篇帖子我都有一次加载。我想在我的页面底部添加分页,并在5之前查看帖子。我有一个自定义的帖子类型,我将其定义为Project。我有两页。主页和日志。主页以我的项目帖子类型为特征,日志以我的常规博客帖子为特征。我希望这两页都有分页。我的当前索引。php是我的主页,如下所示:<?php get_header(); ?> <?php get_footer(); ?> <div class=\"p