WordPress分页无法正确显示



我在博客模板中添加了分页。我的博客每页显示三篇文章。我对分页有意见。

因此,只有当url中的内容为/?s=时,才会显示分页。

例如:http://localhost/sitename/?s=

有人能帮我解决这个问题吗?

<?php
/*
*Template Name: Szablon wyszukiwarki
*Template Post Type: page, post
*/
?>

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

<div class="post-banner">
<div class="post-title">
<h1><?php echo the_title(); ?></h1> 
</div>
</div>


<?php $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
global $post;

$args = array(
'posts_per_page'=>get_option('posts_per_page'),
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
);

$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="col-md">
<a href="<?php the_permalink(); ?>">
<img class="post-thumbnail" src="<?php the_post_thumbnail_url('medium_large'); ?>"/>
</a>
<div class="post-details">
<h6><?php echo get_the_date('j F Y'); ?></h6>
<h6>Nazwa kategorii: <?php echo the_category(' '); ?> </h6>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div>
<?php endforeach; 

posts_nav_link();
wp_reset_postdata();?>


<?php get_footer(); ?>

编辑:
它有效,但为什么我必须在这个代码中设置"1":

'paged' => get_query_var('paged', 1)

这段代码在没有这个参数的情况下运行,为什么?

<?php
/*
*Template Name: Szablon wyszukiwarki
*Template Post Type: page, post
*/
?>

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

<div class="post-banner">
<div class="post-title">
<h1><?php echo the_title(); ?></h1> 
</div>
</div>

<?php 
$query= new WP_Query(array(
'post_type'=>'post', // your post type name
'posts_per_page' => get_option('post_per_page'), // post per page
'paged' => get_query_var('paged', 1)
));
if($query->have_posts()) :
while($query->have_posts())  : $query->the_post(); ?>
<div class="col-md">
<a href="<?php the_permalink(); ?>">
<img class="post-thumbnail" src="<?php the_post_thumbnail_url('medium_large'); ?>"/>
</a>
<div class="post-details">
<h6><?php echo get_the_date('j F Y'); ?></h6>
<h6>Nazwa kategorii: <?php echo the_category(' '); ?> </h6>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p>
<?php the_excerpt(); ?>
</p>
</div>
</div><?php
endwhile;
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text'    => __('« prev'),
'next_text'    => __('next »'),
));

}
?>    
<?php else :?>
<h3><?php _e('404 Error&#58; Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

<?php get_footer(); ?>

$arg数组中,我将执行以下操作:

$args = array(
'posts_per_page' => get_option('posts_per_page'),
'paged' => get_query_var('paged', 1) //This would do the same thing as your ternary operator!
);

然后,我不使用posts_nav_link,而是将max_num_pages值传递给paginate_links函数,如下所示:

echo paginate_links(
array(
"total" => $myposts->max_num_pages
)
);

最新更新