对帖子查询进行分页时出现问题

时间:2020-06-04 作者:Skytribe

我创建了一个帖子模板,用于查找具有相同标签的其他帖子。我想给这张帖子列表分页。分页链接看起来正确,但当我单击它们时,分页丢失了。

例如,页面是mywebsite/category/postname,第二个页面链接指向/mywebsite/category/postname/page/2,但当我单击它时,浏览器地址栏中的URL会返回到mywebsite/category/postname。

在页面模板上使用类似的代码似乎是可行的,所以不确定为什么它不适用于帖子

我在模板中使用的代码如下:

<?php


get_header(); ?>

<div class="container container--has-padding">
<?php if ( has_post_thumbnail() ) { ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>
<div class="single-post-hero" style="background-image:url(<?php echo $url ?>);">
&nbsp;
</div>

<div class="hero-caption"><?php the_field(\'hero_caption\'); ?></div>
<?php } ?>
<?php while ( have_posts() ) : the_post(); ?>
    <div class="single-post-meta">
        <?php the_category(\', \'); ?>
    </div>
    <h1 class="single-post-title"><?php the_title(); ?></h1>

    <div class="single-post-share-top">
        <?php include \'../includes/share.php\'; ?>
    </div>

    <div class="single-post-content">
        <?php the_content(); ?>

    </div>

<div class="posts-archive container section">

    <?php
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; 
    echo \'Currently Browsing Page \', $paged;



    $tags = wp_get_post_tags($post->ID);

    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
    \'tag__in\' => $tag_ids,
    \'post__not_in\' => array($post->ID),
    \'posts_per_page\'=>10, // Number of related posts to display.
    \'caller_get_posts\'=>1
    );
    global $loop;
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        ?>
        <h2 class="text-center">Posts about this manufacturer</h2>
        <ul class="grid">
        <?php
        while ( $loop->have_posts() ) : $loop->the_post();

        $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), \'thumbnail\' ); ?>

        <li class="grid__column grid__column--third">
            <a class="thumbnail" href="<?php the_permalink(); ?>" style="background-image:url(<?php echo $url ?>);">
            <?php the_post_thumbnail(); ?>
            </a>
            <h3 class="grid-posts-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt(); ?>
            <a class="read-more" href="<?php the_permalink(); ?>">Read more</a>
        </li>

    <?php 
        endwhile;


    ?>
        </ul>
    </div>
    <div class="pager">
        <ul>
        <?php

        $total_pages = $loop->max_num_pages;

        if ($total_pages > 1){

            $args = array(
                \'base\' => get_pagenum_link(1) . \'%_%\',
                \'format\' => \'page/%#%\',
                \'total\' => $total_pages,
                \'current\' => $paged,
                \'show_all\' => false,
                \'end_size\' => 3,
                \'mid_size\' => 2,
                \'prev_next\' => True,
                \'prev_text\' => __(\'&lsaquo;\'),
                \'next_text\' => __(\'&rsaquo;\'),
                \'type\' => \'list\',
                );
            echo paginate_links($args);

        }
        ?>
        </ul>
<?php } ?>
    </div>
    <?php
    wp_reset_postdata();
    }
    ?>


    <?php if( get_field(\'show_standard_advert\') ): ?>

        <div class="advert">
            <?php the_field(\'news_category_advert_shortcode\',\'options\'); ?>
        </div>

    <?php endif; ?>

    <div class="advert">
        <?php the_field(\'post_specific_advert_shortcode\'); ?>
    </div>




<?php endwhile; ?>
</div>

1 个回复
SO网友:jannp

您是否尝试添加paged 参数的参数WP_Query?

$args=array(
   \'tag__in\' => $tag_ids,
   \'post__not_in\' => array($post->ID),
   \'posts_per_page\'=>10, // Number of related posts to display.
   \'caller_get_posts\'=>1,
   \'paged\' => $paged
);

相关推荐

Can't Add Pagination

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