Wordpress 分页显示不正确
Wordpress pagination doesn't display correctly
我在我的博客模板中添加了分页。我的博客每页显示三个帖子。我的分页有问题。
因此,分页仅在 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: 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
)
);
我在我的博客模板中添加了分页。我的博客每页显示三个帖子。我的分页有问题。
因此,分页仅在 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: 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
)
);