I added pagination to my blog template. My blog display three posts per page. I have problem with pagination.
So, pagination displays only when in url is /?s=
.
e.g: http://localhost/sitename/?s=
Can anybody help me with this problem?
<?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(); ?>
EDIT:
It works, but why I have to set '1' in this code:
'paged' => get_query_var('paged', 1)
This code run without this parameter, so why?
<?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(); ?>