WordPress custom pagination for custom posts not working - php

I'm developing custom WordPress theme, in that, i'm using custom queries for fetching data and also using pagination for the bulk data.
I have written code for pagination and it is also showing pagination below posts.
my concern is: whenever I click on 2 page (Pagination), it is showing the same post of page 1 (Pagination).
Please check with below code for your reference:-
<div class="blog_area container">
<div class="wrapper row">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'video',
'order' => 'ASC',
'posts_per_page' => 1,
'paged' => $paged
);
$the_query = new WP_Query($args);
// The Query
while ($the_query->have_posts()):
$the_query->the_post();
$vdoLink = simple_fields_value('vdo_link');
$showVdo = $vdoLink;
?>
<div class="main_wrapper col-lg-12 col-md-12 row_wrapper">
<div class="col-md-6">
<div class='embed-container'>
<iframe src="<?php echo $showVdo;
?>" frameborder="0" allowfullscreen></iframe>
</div>
</div>
<div class="col-md-6">
<div class="row ">
<h2 class="margin-top-zero">
<b><?php the_title(); ?></b>
</h2>
</div>
<div class='row'>
<p><?php the_content('Read More');
?></p>
</div>
</div>
</div>
<?php
$post->ID;
endwhile;
$big = 999999999; // need an unlikely integer
?>
<div class="row">
<div class="pagination">
<?php
echo paginate_links(array(
// 'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'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
));
?>
</div>
</div
</div>
</div>

you can try this code.
<?php
$args = array(
'order' => 'DESC',
'showposts' => 1,
'post_type' => 'video',
'paged' => $paged
);
$the_query = new WP_Query($args);
// The Query
if($the_query->have_posts()) : while($the_query->have_posts()): $the_query->the_post(); ?>
<?php the_title(); ?>
<?php
wp_reset_query();
endwhile; else :
echo "no posts!";
endif;
?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
// 'base' => str_replace( $big, '', esc_url( get_pagenum_link( $big ) ) ),
'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
));
?>

Related

Wordpress pagination issue - I set 'posts_per_page' which works for every page except for page 1 which is just showing all posts

I have the following set up in my template file:
<div class="flex" id="allPosts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'posts_per_page' => 12,
'paged' => $paged,
'order' => 'DESC',
);
$wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
<div class="blog-entries">
<?php if (has_post_thumbnail()) {
$featuredImg = get_the_post_thumbnail_url();
} else {
$featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
} ?>
<a class="blog-module" href="<?php the_permalink(); ?>">
<div class="blog-img" style="background-image: url('<?php echo $featuredImg; ?>');">
<div>
<div class="author">By <?php the_author(); ?></div>
<div class="date">
<p><?php echo get_the_date('M'); ?></p>
<p><?php echo get_the_date('d'); ?></p>
</div>
</div>
</div>
<div class="blog-content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
</div>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
This functionality is working, except on page one. Every other page limits the amount of posts seen to 12 as specified in the 'posts_per_page', but page 1 is just showing every post. Ive tried searching around for a similar problem, but have come up short. I would appreciate any and all guidance on what might be happening here, thanks!
You can view the issue here: http://listingmirror.devsite.work/blog/
try in that form
// WP_Query arguments
$args = array(
'nopaging' => false,
'paged' => '12',
'posts_per_page' => '12',
'posts_per_archive_page' => '12',
'order' => 'DESC',
'orderby' => 'title',
);
// The Query
$paged = new WP_Query( $args );
// The Loop
if ( $paged->have_posts() ) {
while ( $paged->have_posts() ) {
$paged->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
The imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument.

Add pagination to archive.php template

I have created an archive template within my bespoke Wordpress site but I need to add pagination - I have tried different plugins/code but nothing seems to work. I currently have 6 posts setup on the backend but I have limited it to 3 posts per page for testing purposes. Existing code below:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 3,
'paged' => $paged
);
$posts_query = new WP_Query($args);
if($posts_query->have_posts()) { ?>
<div class="index-grid">
<div class="grid-column">
<?php while($posts_query->have_posts()) {
$posts_query->the_post(); ?>
<div class="related-item">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('header_image'); ?>" />
<h3 class="title"><?php the_title(); ?></h3>
<p class="blurb"><?php the_field('introduction'); ?></p>
</a>
</div>
<?php } ?>
</div>
</div>
<?php wp_reset_postdata();
}
?>
</section>```
Any help would be greatly appreciated!
Thanks
Try this:
<div class='pagination'>
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $posts_query->max_num_pages
));
?>
<div>

Wordpress custom category.php - pagination link stuck on page 1

I'm developing a custom Wordpress theme. Before this, the pagination link for page 2 shows a 404 error but I somehow managed to fix it but now, clicking page 2 doesn't do anything. It's stuck on page 1. This is my first time attempting to develop a custom theme from scratch. Help is greatly appreciated.
Here is my category.php
<?php
get_header();
$currCat = get_category(get_query_var('cat'));
$cat_name = $currCat -> name;
$cat_id = get_cat_ID($cat_name);
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$wp_query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 6,
'cat' => $cat_id,
'paged' => $paged));
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '/page/%#%',
'current' => max(1, $paged),
'prev_text' => __('Previous Page'),
'next_text' => __('Next Page'),
'show_all' => true,
'total' => $query->max_num_pages ));?>
<div id="primary" class="content-area container">
<div id="content" class="site-content" role="main">
<div class="post-category">
<h2 class="post-category-title"><span class="post-category-title-m"><?php single_cat_title();?></span></h2>
</div>
<div class="post-items">
<?php if ($wp_query-> have_posts() ) : ?>
<?php while ($wp_query -> have_posts() ) : $wp_query -> the_post(); ?>
<div class="post-item">
<div class="post-thumbnail"><?php the_post_thumbnail('single-thumbnail'); ?></div>
<div class="post-title"><?php the_title(); ?></div>
<strong>
<?php the_date( 'F j, Y','<div class="post-date">','</div>'); ?>
<div class="post-excerpt"><?php the_excerpt();?></div>
</strong>
</div>
<?php endwhile;
else :
// No Post Found
endif; ?>
</div>
<nav class="page_nav">
<?php custom_pre_get_posts($wp_query); ?>
</nav>
<!-- <?php //wp_reset_postdata(); ?> -->
</div>
</div>
<?php get_footer();?>
And the function in my functions.php
function custom_pre_get_posts($query)
{ global $paged;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'prev_text' => __('Previous Page'),
'next_text' => __('Next Page'),
'show_all' => true,
'total' => $query -> max_num_pages
));
if ($query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {
$query->set('page_val', get_query_var('paged'));
$query->set('paged', 0);
}
}
add_action('pre_get_posts', 'custom_pre_get_posts');

How can I use paginate_links without getting a 404 error in wordpress?

I'm trying to build a correct pagination for a custom post type archive and the only function that seems to work in this case is paginate_links(). The problem is that when I click "Older posts" or second page I get a 404 error.
I have a custom loop with wp_query.
I can't understand where the problem can be, I already tried changing permalinks but it didn't work. Anybody have any idea of how to fix this? Thank you in advance guys!
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged
));
?>
<div class="container">
<div class="row servizi">
<?php if ($query -> have_posts()) : ?>
<?php while($query -> have_posts()) : $query -> the_post(); ?>
<div class="col-lg-4">
<div class="ratio-box">
<div class="inner-ratio-box">
<?php if(has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" rel="nofollow">
<?php the_post_thumbnail('large', array('class' => 'responsive inner-img')); ?>
</a>
<?php endif; ?>
</div>
</div>
<p><?php the_title();?>
<br><?php the_category(', '); ?>
<br><?php $archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_day = get_the_time('d'); ?>
<?php the_date('d/m/Y'); ?></p>
</div>
<?php endwhile; ?>
<?php // next_posts_link() usage with max_num_pages.
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
// Clean up after the query and pagination.
wp_reset_postdata(); ?>
<?php endif; ?>
</div>

Wordpress custom post type pagination shows same content on each page

I have a custom post type name portfolio.
I have applied pagination successfully but content on each page is the same.
Below is my code.
I have tried too many solutions but unable to find appropriate answer.
please help
<?php
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
// The Loop
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<div>
// The content
</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' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>
Try this code,
$the_query = new WP_Query( array('posts_per_page'=>10,
'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 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;
$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();
Try this
<?php
/*
Template name: Test portfolio
*/
$exec_query = new WP_Query( array (
'posts_per_page'=>10,
'post_type' => 'portfolio',
'job_role' => 'executive',
'post-thumbnails' => 'thumbnail',
'paged' => get_query_var('paged') ? get_query_var('paged') : 1) );
if ( $exec_query->have_posts() ) {
while ( $exec_query->have_posts() ): $exec_query->the_post(); ?>
<p><?php echo get_the_title(); ?></p>
<div><?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' => $exec_query->max_num_pages
) );
wp_reset_postdata();
}
?>

Categories