How to implement wordpress pagination - wordpress

I have looked for pagination scripts and put them in the code below but nothing works, how do I implement pagination below?
'<?php
$args = array( 'posts_per_page' => 10 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php endforeach;
wp_reset_postdata(); ?>'

Try This :
You need to pass paged parameter inside argument.and then pass max_num_pages in function paginate_links
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 10 ,'paged'=>$paged);
$the_query = new WP_Query( $args );
while ($the_query ->have_posts()) : $the_query -> the_post();
?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_query();

Related

pagination for custom post type archive page

I'm trying to add pagination to following code inside archive-CustomPostType.php page but it doesn't show pagination or it shows empty page.
My code:
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif;
?>
</div>
<?php get_footer(); ?>
I'm using page navi for pagination but I don't think it's the problem because I tried with normal wordpress pagination but didn't help.
I think the issue is coming from get_queried_object and I don't know how to handle this.
Any help would be appreciated.
You Can Try This Code.
<?php get_header(); ?>
<h2 class="ptm pbm mbl text-right red-bg"><span>سریال</span></h2>
<div class="container">
<?php
$terms = get_queried_object();
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$posts = get_posts(array(
'post_type' => 'series',
'posts_per_page' => 20,
'paged' =>$paged,
'category' => $terms->term_id,
'meta_query' => array(
array(
'key' => 'is_season',
'compare' => '==',
'value' => '0'
)
)
));
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<!--HTML goes here-->
<?php endforeach; ?>
</ul>
<div class="pagination">
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => 'Previous',
'next_text' => 'Next',
) );
?>
</div>
<?php else : ?>
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
<?php endif; ?>
?>
</div>
<?php get_footer(); ?>
Modifying query post type in functions.php
add_action( 'pre_get_posts' ,'wpse222471_query_post_type_portofolio', 1, 1 );
function wpse222471_query_post_type_portofolio( $query )
{
if ( ! is_admin() && is_post_type_archive( 'customposttype' ) && $query->is_main_query() )
{
$query->set( 'posts_per_page', 6 ); //set query arg ( key, value )
}
}

How to include pagination in a Wordpress Custom Post Type Query

I have the code below:
<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php echo $file; ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i>
<?php echo get_the_title(); ?>
</a>
<div class="file-description">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
I am trying to use paginate_links Wordpress function but no matter where I put it, I can't make it work. Can someone help me with this?
Try the code below:
$the_query = new WP_Query( array('posts_per_page'=>30,
'post_type'=>'phcl',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
);
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
When querying a loop with new WP_Query set the 'total' parameter to the max_num_pages property of the WP_Query object.
Example of a custom query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post', // Your post type name
'posts_per_page' => 6,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
// YOUR CODE
endwhile;
$total_pages = $loop->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 »'),
));
}
}
wp_reset_postdata();
?>
Example of paginate_links parameters adapted to the custom query above:
For more reference please visit this link
Try This one, it worked for me!
<?php
$paged = get_query_var('paged');
$args = array(
'post_type' => 'blogs',
'paged' => $paged,
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 1,
'post_status' => 'publish',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
?>
Your code...
<?php
}
}
wp_reset_query();
?>
<div class="pagination">
<?php
echo "<div class='fz-pagination'>" . paginate_links(array(
'total' => $the_query->max_num_pages,
'prev_text' => __('<div class="preious-page">Prev</div>'),
'next_text' => __('<div class="next-page">Next</div>')
)) . "</div>";
?>
</div>

Wordpress pagination links show same posts as main page why?

I am using the following code it works and gives the pagination links, unfortunately each pagination page has same content and main page why?
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 3 ,'paged'=>$paged);
$the_query = new WP_Query( $args );
while ($the_query ->have_posts()) : $the_query -> the_post();
?>
<div id="pbox">
<div id="pthumb"><?php the_post_thumbnail('thumbnail', array('class' => 'mythumbnail')); ?></div>
<div id="pcontent">
<?php the_title(); ?>
<?php the_excerpt(); ?><br />
Post Category: <b><?php the_category( ', ' ); ?></b>
</div>
</div>
<?php
endwhile;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
echo $paged;
wp_reset_query(); ?>

To get pagination in my wiget custom post type list

I have get the custom post type by category in widget. Now i want to paginate those post if those more than 10 posts.For that i tried with this code
<ul class="posts-list">
<?php
$cats = get_the_category();
$cat_name = $cats[0]->name;
$paged= isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
while (have_posts()) : the_post();
// do whatever you want
?>
<li>
<h5><?php the_title(); ?></h5>
<p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
<p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p>
<?php
endwhile;
?>
</li>
<?php
$pag_args1 = array(
'format' => '?paged=%#%',
'current' => $paged,
'total' => 3,
'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>
But i cant get the exact result.
you may try like the below
<ul class="posts-list">
<?php
$cats = get_the_category();
$cat_name = $cats[0]->name;
if ( get_query_var('paged') )
{ $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
query_posts(array ( 'category_name' => $cat_name, 'posts_per_page' => 6, 'paged' => $paged));
while (have_posts()) : the_post();
// do whatever you want
?>
<li>
<h5><?php the_title(); ?></h5>
<p><strong>posted on</strong> <?php the_time(' F jS, Y') ?> <strong>by</strong> <?php the_author(); ?></p>
<p><strong>Posted in</strong> <span><?php print_r($cat_name); ?></span></p>
<?php
endwhile;
?>
</li>
<?php
$pag_args1 = array(
'format' => '?paged=%#%',
'current' => $paged,
'total' => 3,
'add_args' => array( 'paged' => $paged)
);
echo paginate_links( $pag_args1 ); ?>
</ul>

Display two posts from each post type with wp_query

I have used the following code to display the one post from each post.
$post_types = array('a', 's','d','f','g');//post type names
foreach( $post_types as $post_type) :
// The Query
$the_query = new WP_Query( array( 'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="issue-content masonItem <?php echo $post_type; ?>">
<?php
$url = wp_get_attachment_url( get_post_thumbnail_id() );
$resizedUrl = get_bloginfo('template_url')."/libs/timthumb.php?src=".$url."&w=327&h=204&zc=1"; ?>
<img src="<?php echo $resizedUrl; ?>" alt="<?php the_title(); ?>" class="masonry-thumb" />
<?php get_template_part( 'content-issues', 'page' );?>
</div><!--issue-content--><!--Mason Item-->
<?php endif; ?>
<?php endwhile;
Now i want to display 2 posts from each post type. How can i do this ? I got the $post_count idea. But i cant know use it in my code.
Try this :
'posts_per_page' => 2,
You try This
$the_query = new WP_Query( array( 'post_type' => $post_type,'orderby' => 'post_date','posts_per_page' => 2,'order' => 'DESC'));

Resources