分页坏了,我需要帮助修复它

时间:2017-10-18 作者:NeoLobster

我在一个正在工作的站点上遇到分页问题。客户注意到,在她的分类页面上,分页在page 1. 我的意思是开着pages 2 然后,帖子不再显示在WordPress循环中。我只是想知道为什么会这样,我怎么才能解决它?

以下是类别页面的模板页面代码:

<?php
get_header();?>
<div class="section blog">
    <div class="container">
        <div class="row">
            <div class="col-xs-12 col-md-8">
                <div class="share">
                        <span>Share:</span>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-twitter fa-stack-1x"></i>
                            </span>
                        </a>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-facebook fa-stack-1x"></i>
                            </span>
                        </a>
                        <a href="#" target="_blank">
                            <span class="fa-stack fa-lg">
                                <i class="fa fa-circle fa-stack-2x"></i>
                                <i class="fa fa-linkedin fa-stack-1x"></i>
                            </span>
                        </a>
                </div>
                        <?php

                            $link = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
                            $link_array = explode(\'/\',$link);
                            $page = $link_array[count($link_array)-2];

                            $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;

                            $args = array(
                            \'post_type\' => \'post\',
                            \'posts_per_page\' => 4,
                            \'paged\' => $paged,
                            \'category_name\' => $page
                            );

                            $post_query = new WP_Query( $args );

                            if($post_query->have_posts() ) {
                                while($post_query->have_posts() ) {
                                    $post_query->the_post();
                                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' );
                                    if($image[0] == \'\'){
                                        $image[0] = get_template_directory_uri() . \'/img/placeholder-blog.jpg\';
                                    }
                        ?>

                <article>
                    <div class="post">
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <div class="head">  
                                    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                                    <span><?php the_time(\'F j, Y\'); ?> by <?php echo get_the_author(); ?> | <a href="<?php the_permalink(); ?>">Leave a Comment</a></span>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 col-md-3 feature-image">
                                <img src="<?php echo $image[0]; ?>" alt="">
                            </div>
                            <div class="col-xs-12 col-md-9">
                                <p><?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, \'...\'); ?></p>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 col-md-12">
                                <div class="category">
                                    <p>
                                        Filed Under: 
                                        <?php
                                            $category = get_the_category();
                                            foreach($category as $key=>$cat){
                                                echo \'<a href="\' . get_site_url() . \'/blog/\' . $cat->slug . \'">\' . $cat->name . \'</a>\';
                                                if(($key+1) != count($category)){
                                                    echo \', \';
                                                }
                                            }
                                        ?>
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </article>
                <hr>
                        <?php
                                }
                            }
                        ?>


                        <div class="section nav text-center">
                            <!-- pagination here -->
                            <?php
                                if (function_exists(custom_pagination)) {
                                custom_pagination($custom_query->max_num_pages,"",$paged);
                                }
                            ?>

                        </div>


            </div>
            <div class="col-xs-12 col-md-4">
                <?php include(\'inc/sidebar.php\'); ?>
            </div>
        </div>
    </div>
</div>
<?php 
include(\'inc/connect.php\');
get_footer(); 
?>
函数中还内置了一个自定义函数。用于分页的php可能还包含以下问题:

function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {

  if (empty($pagerange)) {
    $pagerange = 2;
  }

  /**
   * This first part of our function is a fallback
   * for custom pagination inside a regular loop that
   * uses the global $paged and global $wp_query variables.
   * 
   * It\'s good because we can now override default pagination
   * in our theme, and use this function in default quries
   * and custom queries.
   */
  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == \'\') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    }
  }

  /** 
   * We construct the pagination arguments to enter into our paginate_links
   * function. 
   */
  $pagination_args = array(
    \'base\'            => get_pagenum_link(1) . \'%_%\',
    \'format\'          => \'page/%#%\',
    \'total\'           => $numpages,
    \'current\'         => $paged,
    \'show_all\'        => False,
    \'end_size\'        => 1,
    \'mid_size\'        => $pagerange,
    \'prev_next\'       => True,
    \'prev_text\'       => __(\'<i class="fa fa-angle-left"></i>\'),
    \'next_text\'       => __(\'<i class="fa fa-angle-right"></i>\'),
    \'type\'            => \'plain\',
    \'add_args\'        => false,
    \'add_fragment\'    => \'\'
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class=\'custom-pagination\'>";
      echo $paginate_links;
    echo "</nav>";
  }

}
以下是此类活动的实例:

https://www.staceybarr.com/measure-up/category/podcast/page/2/
https://www.staceybarr.com/measure-up/category/podcast/page/1/
在我看来,一切都是正确的,但遗憾的是,仍然有一个问题,我只是想知道我是否可以得到一些帮助?非常感谢您的帮助!

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

我不知道为什么第一页有效,但第二页无效,尽管这可能是对问题的暗示。在类别模板页面的底部,您可以看到以下内容:

if (function_exists(custom_pagination)) {
    custom_pagination($custom_query->max_num_pages,"",$paged);
}
如果您查看以上内容,则有一个变量称为$custom_query. 这看起来像是WordPress查询,应该能够在其中包含max\\u numb\\u pages项。问题是$custom_query 不存在于页面上的任何其他位置,并且似乎不是全局的。您应该更改它以在页面上反映您的查询,$post_query. 更新后的代码如下所示:

if (function_exists(custom_pagination)) {
    custom_pagination($post_query->max_num_pages,"",$paged);
}
这应该告诉分页有许多页面,并可能呈现正确的链接。

此外,我建议您print_r 的输出$link, $link_array, $page, 和$paged 确定所有这些变量是否输出正确的响应。

$link = $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$link_array = explode(\'/\',$link);
$page = $link_array[count($link_array)-2];

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;

$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 4,
\'paged\' => $paged,
\'category_name\' => $page
);

print_r($link);
print_r($link_array);
print_r($page);
print_r($paged);
print_r($args);
在以下情况下$args, 确保它告诉WordPresspaged 属性

结束

相关推荐

Url outside the loop

如何在循环外获取缩略图和单页链接的url。对于缩略图,我尝试了 wp_get_attachment_url(); 但这对我不起作用。