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

时间:2019-07-22 作者:Sujan Shrestha

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

<?php get_header();?>

<?php get_template_part(\'navigation\'); ?>

<?php get_template_part(\'blogslist\'); ?>

<?php get_footer();?>
博客列表的主代码位于页面博客列表中。php

<div class="col-md-9">

        <!-- INDIVIDUAL BLOG ITEM LIST -->

          <?php
            $args = array(
            \'posts_per_page\' => 2,
            \'post_type\'     => \'blog\', 
            \'orderby\'       => \'date\',
            \'order\'         => \'DESC\' 
            );

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

            while($loop->have_posts()) : $loop->the_post();
          ?>
          <div class="blog-item">

          <div class="blog-item__top">
            <div class="blog-item__image">
              <a href="<?php the_permalink();?>">
                <?php $image = get_field(\'image\');
                if( !empty($image) ): ?>

                  <img src="<?php echo $image[\'url\']; ?>" class="img-responsive" alt="<?php echo $image[\'alt\']; ?>" />

                <?php endif; ?>
              </a>
            </div>
        <div class="blog-item__detail">
          <ul>
            <li><a href="#"><i class="fa fa-calendar" aria-hidden="true"></i><?php the_time(\'M j, Y\');?></a></li>
            <li><a href="#"><i class="fa fa-thumbs-up" aria-hidden="true"></i>201 LIKES</a></li>
            <li><a href="#"><i class="fa fa-comment" aria-hidden="true"></i>15 COMMENTS</a></li>
          </ul>
        </div>
      </div>

      <div class="blog-item__bottom">
        <div class="row">
          <div class="col-md-3 blog-item__bottom_left">
            <h5>Author: <a href="<?php echo get_author_posts_url(get_the_author_meta(\'ID\'))?>"><?php the_author();?> </a></h5>
            <h6>Category: Football</h6>
          </div>
          <div class="col-md-9 blog-item__bottom_right">
            <h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
            <p>
              <?php echo get_excerpt(500); ?>
            </p>
            <div class="button-div">
                <div class="primary-button">
                  <a href="<?php the_permalink(); ?>"> Read More</a>
                </div>
            </div>
          </div>
        </div>
      </div>

    </div>
      <?php  endwhile;
        }
      ?>
我正在使用WP初学者的分页代码,我将其放入函数中。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 \'<div class="navigation"><ul>\' . "\\n";

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

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

        printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( 1 ) ), \'1\' );

        if ( ! in_array( 2, $links ) )
            echo \'<li>…</li>\';
    }

    /** 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( \'<li%s><a href="%s">%s</a></li>\' . "\\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 \'<li>…</li>\' . "\\n";

        $class = $paged == $max ? \' class="active"\' : \'\';
        printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

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

    echo \'</ul></div>\' . "\\n";

}
因为我使用的是自定义查询循环,所以我在函数wp初学者\\u numeric\\u posts\\u nav()中使用了$循环。但仍然没有显示我的分页。有人能帮我找到问题吗。我真的被困在这里面了。

1 个回复
最合适的回答,由SO网友:Sohan Zaman 整理而成

The solution:

问题是$loop 变量不在全局范围内。所以你需要将它添加到你的全局中blogslist.php 通过添加文件global $loop; 之前$loop = new WP_Query($args); 线

More Efficient Way:

或者,您也可以通过$loop 作为这样的论点:

function wpbeginner_numeric_posts_nav($loop) {
// Codes here.. you also need to remove "global $loop" if you\'re using this method
}
那就这样叫它进来blogslist.php 文件:

wpbeginner_numeric_posts_nav($loop);

Simple Way:

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

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“。

Edit:

您必须使查询侦听分页,更改查询参数如下:

$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的单个页面:

if( is_singular() )
        return;

相关推荐

WooCommerce Custom Pagination

有人能帮我解决我的定制归档产品的问题吗。php?我正在尝试将分页添加到我制作的网格中,但不知道从何处开始。这是我的产品代码:<?php $the_query = new WP_Query( array( \'posts_per_page\'=>12, \'post_type\'=>\'product\', \'paged\' => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1