【问题标题】:WordPress Category Pagination not workingWordPress类别分页不起作用
【发布时间】:2015-05-06 04:10:41
【问题描述】:

我有两种来自archive.php 的自定义帖子类型,一种是archive-slug.php,另一种是category-slug.php。分页适用于archive-slug.php,但category-slug.php 上的相同代码甚至不会显示。我对 Wordpress 和 php 有点陌生,所以我确定我在这里遗漏了一些东西,我只是不知道是什么?

<?php 
// Custom Post Type
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'post_type' => 'available_dogs',
        'category_name' => 'adopted-dogs',
        'posts_per_page'=> 9,
        'paged'=> $paged
    );
    $the_query = new WP_Query( $args ); ?>
        <?php if ( $the_query->have_posts() ) : ?>
            <?php while ( $the_query->have_posts() ) : $the_query->the_post();     ?>   
            <div class="dog-info-box col-lg-4 col-sm-6 col-xs-12">...</div>  
            <?php endwhile; ?>
            <!-- end of the loop -->
            <?php wp_reset_postdata(); ?>
        <?php else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>
    </div>
<?php 
// Get the pagination
fusion_pagination( $pages = '', $range = 2 );
?> 
<?php if( $sidebar_exists == true ): ?>
<?php wp_reset_query(); ?>

【问题讨论】:

  • category_name 将类别 SLUG 而不是 name 作为属性

标签: php wordpress pagination


【解决方案1】:


我想告诉你,当你创建一个文件时“

category-slug.php : 默认模板,无需添加查询帖。
它将显示在 category-slug.php 等文件中添加了 slug 的类别的帖子。
这是修改代码。 if (have_posts() ) :while ( have_posts() ) : the_post(); the_content();//content is going on. endwhile;
//pagination function
fusion_pagination( $pages = '', $range = 2 );
else : _e( 'Sorry, no posts matched your criteria.' );
endif;

注意事项:1:分页代码应放在 while 循环之前。
2:在您的代码中,分页代码已放在 endif 之后,这就是它不起作用的原因。
感谢您。

【讨论】:

  • 我已经删除了查询帖子,但现在除了“抱歉,没有符合您的条件的帖子”之外没有显示任何内容。如果没有查询,它就会中断。当我保持查询不变,并将分页移到 endif 之前。它仍然没有显示。
  • 请看下面的答案。
【解决方案2】:

我使用了这段代码并让它工作。它不漂亮,但现在可以了。出于什么原因,当我尝试在循环之后使用它时它不起作用。当我在 endif 之后使用它时。

    <?php if ($the_query->max_num_pages > 1) { // check if the max number of    pages is greater than 1  ?>
      <nav class="prev-next-posts">
         <div class="prev-posts-link">
            <?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
         </div>
         <div class="next-posts-link">
            <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
         </div>
      </nav>
    <?php } ?>

【讨论】:

    【解决方案3】:

    请按照以下步骤操作。 1:请确保您的类别有帖子。
    2:如果你的分类标签是“test”,那么你的文件名应该是“category-test.php”。如果有任何混淆,请告诉我。

    3:查看代码并写入文件。

    if(have_posts()) :
    while(have_posts()): the_post();
    the_title();
    the_content();
    endwhile;
    //pagination of wp previous_posts_link( '« Newer Entries' ); next_posts_link( 'Older Entries »', 0 );
    endif;

    注意:
    1:请将此代码放入文件中运行,如果运行成功,我们将添加设计部分。
    2:如果你可以分享网站网址,那么我也可以提供合适的解决方案,因为我可以看到类别 slug 等。
    如果仍然需要问题,请告诉我。
    谢谢。

    【讨论】:

      【解决方案4】:

      我终于修复了这个问题的变体。

      这个问题发生在分类存档分页上。

      最初认为它在循环中 - 结果我正在研究的主题将这些过滤器放在一个文件 custom.php 中,该文件手动重写了永久链接结构。

      add_filter('category_link', 'themename_change_archive_link', 100, 2);
      add_action('init', 'themename_archive_rewrite', 50);
      

      由于看起来像conflict with a recent WooCommerce update,分页不喜欢这样,所以我删除了这些过滤器,使用自定义永久链接插件重写了类别永久链接,并且成功了!现在我有一个自定义的永久链接结构和功能类别分页。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 2013-11-25
        • 2018-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多