"How to fix 'Custom pagination 404 error' in PHP - wordpress

I'm facing problem in my custom post pagination. The custom pagination is work fine on single page with same code but it is not working on another page.
I have updated the permalink many times but nothing happen. I also update .htaccess file.
My source code listed here...
<div id="home" class="tab-pane fade in active">
<p class="main_dv"><?php
//$count=1;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$data= new WP_Query(array(
'post_type'=>'software',// your post type name
'category_name' => 'general_software', // your category name
'posts_per_page' => 5, // post per page
'paged' => $paged,));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();?>
<div class="testcustompost1">
<div class="pst_div">
<div class="thumb">
<?php the_post_thumbnail();?>
</div>
<div class="title_des">
<div class="title">
<?php the_title();?>
</div>
<div class="description">
<?php the_content();?>
</div>
<div class="downlod_btn">
<button class="btn"> Download </button>
</div>
</div>
</div>
</div>
<?php //$count++;
endwhile;
echo '<div class="paginat_design">';
$total_pages = $data->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 »'),
));
}
echo '</div>'?>
<?php else :?>
<?php _e('404 Error: Not Found', ''); ?>
<?php endif; ?>
<?php wp_reset_postdata();?>
</p>
</div>
404 Not found error

You should try this:
<?php
/**
* Looping through the content
*/
$page = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$data = new WP_Query (array(
'post_type'=>'software',
'category_name' => 'general_software',
'posts_per_page' => 5,
'page' => $page
)); ?>
<?php while ($data -> have_posts()) : $data -> the_post(); ?>
<!-- Your html code here -->
<?php endwhile; ?><?php wp_reset_query(); ?><?php wp_reset_postdata();
?>
Here is the pagination part:
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $data->max_num_pages,
'current' => max( 1, get_query_var( 'page' ) ),
'format' => '?page=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Previous Page', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Next Page', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
Let me know if it is this help you. If not, try to change the value from paged to page.

Related

Wordpress Custom post page with wp_pagenavi doesn't work

I have custom page for posts which has to show up 9 posts per page and I used wp_pagenavi plugin but it doesn't work. Please help!
<div class="page-content__wrapper">
<?php
$post_category = get_field('page_category');
$posts = get_posts( array(
'numberposts' => 9,
'category_name' => $post_category,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
//...posts
<?php
}
wp_pagenavi();
wp_reset_postdata();
?>
</div>
I used WP_Query() to solve this problem (without wp_pagenavy plugin :)
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup
$post_category = get_field('page_category'); // getting category name
$posts= new WP_Query(array(
'post_type'=> 'post',
'posts_per_page' => 9,
'paged' => $paged, // don't forget to give this argument
'category_name' => $post_category,
));
if($posts->have_posts()) :
while($posts->have_posts()) : $posts->the_post(); ?>
// All posts will be here!
<?php endwhile; ?>
<div class="mt-30 text-center">
<?php
$GLOBALS['wp_query'] = $posts;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> <',
'next_text' => '> <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '));
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Wordpress: authors page with custom query

On a wordpress site I am coding an authors page. Therefore I use a custom query that I also use in several other parts of the site. Partly it works fine, but as it has a major bug, I cannot use it:
I receive not only a chosen autors post. All posts of all authors are listet.
If I use the main loop, I do not have that problem. But because of the pagination which should be the same everywhere, I would like to use the custom loop. Where is my mistake?
Here my code:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
?>
<section class="tk_fullwidth">
<div id="primary" class="content-area">
<h1 class="page-title">Beiträge von: <?php echo $curauth->nickname; ?></h1>
<div id="main" class="site-main" role="main">
<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => '50',
'paged' => $paged,
'order' => 'DESC',
'orderby' => 'date',
) );
?>
<!-- begin loop -->
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="grid-item">
<a class="xyz" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
</div><!-- grid-item -->
<?php endwhile; ?>
<!-- end loop -->
<div class="tk_pagination">
<?php
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', __( '<', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( '>', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div><!-- #main -->
</div><!-- #primary -->
</section><!--tk_fullwidth-->

Pagination does not work, I consider fixing or new coding

I coded a wordpress-site and since some time the pagination worked fine. Then it stopped working.
I tried different plugins, but no one works. I want to put the pagination in a special place within a self-coded loop. Therefore the plugin of bestwebsoft seemed to be the best, since it offers the option of displaying the pagination by the use of a bit of PHP. But also that does not work. The support of bestwebsoft is not helpful, they only repeat what I can read in the documentation.
My questions:
1) Is there eventually a problem in my loop, that stops the pagination from displaying?
2) Is it a problem that I use "Masonry" for showing the excerpts and order the excerpts in a horicontal manner? Maybe that interferes with the pagination?
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
if ( function_exists( 'pgntn_display_pagination' ) ) {
pgntn_display_pagination( 'posts' );
}
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->
I want to have some kind of pagination. I would prefer a self-coded one, but could not do it. Therefore I also would accept a plugin-solution. but nothing works! Most of all i would like to know where the problem is located.
I don't know what is the code in pgntn_display_pagination function but I have modified your code with below one which will work for you.
Kindly check below code.
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // get the current page variable and set it
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
'paged' => $paged // use $paged variable here
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
// Below is full code of pagination
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', __( 'Next', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Previous', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->

Wordpress custom post type pagination 404 page 2

I'm a novice and I'm trying to develop my first theme.
Unfortunately I'm experiencing some troubles with custom post type (projet). I have tried different combinations found about this topics (query_post, WP_Query, flush permalinks, etc.), but none seems to work for me.
I try to use the plugin wp_pagenavi.
Here is my register post type, in function.php :
register_post_type('projet', array(
'label' => __('Réalisations'),
'singular_label' => __('Projet'),
'public' => true,
'show_ui' => true,
'rewrite' => array('slug' => 'international/realisations', 'with_front' => true),
'hierarchical' => false,
'has-archive' => true,
'query_var' => true,
'supports' => array('title', 'excerpt', 'editor', 'thumbnail', 'page-attributes')
));
Here is the code from the template that list my custom post type "projet":
<ul id="list-projets" class="grid cs-style-7">
<?php if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
} ?>
<?php
$args= array(
'showposts' => 2,
'posts_per_page' => 2,
'post_type' => 'projet',
'paged' => $paged
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post(); ?>
<li>
<figure>
<?php the_post_thumbnail(); ?>
<h3><?php the_title(); ?></h3>
<figcaption>
<span><?php echo get_the_excerpt(); ?></span>
+ de détails
</figcaption>
</figure>
</li>
<?php endwhile; ?>
<?php endif;
wp_reset_query(); ?>
<nav>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
</nav>
</ul>
Currently this code lists the 2 last custom post type "projet" and the pagination, but if I click on the page 2 it displays a 404 error. The url is /localhost/wordpress/international/realisations/page/2/
I tried to add 'has_archive' => true to my register post type, but when I save and flush the permalinks and return to my listing page /localhost/wordpress/international/realisations/ the page is empty and my breadcumb is no more good "ACCUEIL > RÉALISATIONS" instead of "ACCUEIL > INTERNATIONAL > RÉALISATIONS".
I'm a novice so maybe I did something wrong, please help me because I've been searching for a few days and I don't know what to do !
I also tested this solution (still 404), at this link.
Insted of this
<nav>
<?php wp_pagenavi( array( 'query' => $the_query ) ); ?>
</nav>
Use this
<div class="pagination">
<?php
global $wp_query;
$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' => $wp_query->max_num_pages
) );
?>
</div>

Wordpress wp_query custom post type query not working on second page

Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$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' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>

Resources