首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在自定义博客页面模板中添加分页

在自定义博客页面模板中添加分页
EN

WordPress Development用户
提问于 2019-07-22 18:11:31
回答 1查看 2.5K关注 0票数 0

我正在为博客使用自定义页面( page -allblogs.php)。我没有写这个页面中的所有代码,因为我想要写干净的代码。因此,本页有以下代码:

代码语言:javascript
运行
复制

博客列表的主要代码在页面bloglist.php中。

代码语言:javascript
运行
复制
           2,
            'post_type'     => 'blog', 
            'orderby'       => 'date',
            'order'         => 'DESC' 
            );

            $loop = new WP_Query($args);
            if($loop->have_posts()) {

            while($loop->have_posts()) : $loop->the_post();
          ?>
          

          
            
              
                

                  

                
              
            
        
          
            
            201 LIKES
            15 COMMENTS
          
        
      

      
        
          
            Author:  
            Category: Football
          
          
            
            
              
            
            
                
                   Read More
                
            
          
        
      

    
          I am using wpbeginner's pagination code which i have put in functions.php.function wpbeginner_numeric_posts_nav() {

 if( is_singular() )
        return;


    global $wp_query, $loop;
if ( $loop ) {
    $total = $loop->max_num_pages;
} else {
    $total = $wp_query->max_num_pages;
}

    /** Stop execution if there's only 1 page */
    if( $total <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $total );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '%s' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '%s' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '…';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '%s' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '…' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '%s' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '%s' . "\n", get_next_posts_link() );

    echo '' . "\n";

}由于我使用的是自定义查询循环,所以在函数wpbeginner_numeric_posts_nav()中使用了wpbeginner_numeric_posts_nav()。但我的页码仍然没有显示出来。有人能帮我找到这个问题吗。我真的被困在这里了。
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-07-23 11:03:43

The解决方案:

问题是$loop变量不在全局范围内。因此,您需要将它添加到blogslist.php文件中的全局文件中,方法是在$loop = new WP_Query($args);行之前添加global $loop;

More有效方式:

或者,您也可以将$loop作为如下参数传递:

代码语言:javascript
运行
复制
function wpbeginner_numeric_posts_nav($loop) {
// Codes here.. you also need to remove "global $loop" if you're using this method
}

然后在blogslist.php文件中这样调用它:

代码语言:javascript
运行
复制
wpbeginner_numeric_posts_nav($loop);

Simple方式:

还有另一种简单的方法,您根本不需要任何自定义函数。在您的blogslist.php文件中,结束后:

代码语言:javascript
运行
复制
global $wp_query;

$wp_query_backup = $wp_query; // Take backup of the $wp_query
$wp_query = $loop; // Set your custom query as global $wp_query

the_posts_pagination(); // Calling the pagination, you can use it as you use in index or archive page, you may also use the_posts_navigation() here.

$wp_query = $wp_query_backup; // Restore the backup
wp_reset_query(); // Not really necessary but just to be sure

但是,如果您正在为任何存储库开发主题,则不建议采用这种方式,因为它违反了“永远不要修改$wp_query”的准则。

编辑:

必须让查询侦听分页,将查询args更改如下:

代码语言:javascript
运行
复制
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
    'posts_per_page' => 2,
    'post_type'     => 'blog', // I'm not sure if you're using custom post type or not. If it's default blog posts, then 'post_type' should be 'post' not blog. Or just don't add anything as post_type default is regular post
    'orderby'       => 'date',
    'order'         => 'DESC' 
    'paged' => $paged
) );

另外,从您的自定义函数中删除代码以获得页面模板中的分页,因为它是WordPress的一个页面:

代码语言:javascript
运行
复制
if( is_singular() )
        return;
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/343424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档