我创建了一个帖子模板,用于查找具有相同标签的其他帖子。我想给这张帖子列表分页。分页链接看起来正确,但当我单击它们时,分页丢失了。
例如,页面是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 ?>);">
</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\' => __(\'‹\'),
\'next_text\' => __(\'›\'),
\'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>