WordPress pagination shows 404 error - php

I've the following code (simplified as I have some HTML between the PHP tags):
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php $args = array( 'posts_per_page' => 1, 'paged' => $paged, ); ?>
<?php query_posts($args); ?>
<?php if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content() ?>
<?php endwhile; ?>
<?php endif; ?>
<?php the_posts_pagination(); ?>
The problem is, when I click on the next page then it shows my 404 page.
This code is placed in the home.php and it is my news page.
How to fix this issue?
I've tried a lot queries on the internet, and already changed the permalink structure to default.

Related

What is the right way to present custom post type in WordPress?

PRECONDITION:
I created custom post type "comparison"
I created 'widget.php' and registered it in 'functions.php'
I added widget to 'front-page.php'
TASK:
On my Front Page and I want to list all posts with type "comparison" in separate widget with pagination
PROBLEM:
I don't know the 'best practice', how to implement it technically. How to list all "comparison" in widget?
Here is the custom post template code, You can assign the template on the page you want to display the posts list. You can try with below template code:
<?php
# Template Name: Comparison List
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 15,
'paged' => $paged,
'post_type' => 'comparison',
'orderby' => 'title',
'order' => 'asc'
);
$wp_query = new WP_Query($args);
?>
<?php get_header(); ?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<?php get_template_part( 'template-parts/content', 'comparison-list-item' ); ?>
<?php endwhile; ?>
<?php print_pagination($wp_query); ?>
<?php else: ?>
<?php get_template_part( 'template-parts/content', 'nothing-found' ); ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
<?php get_footer(); ?>

Add Pagination to Woocommerce Custom Template

Total noob here. I checked all over the net but I'm finding it difficult to add the pagination feature to my homepage. I'm using the recent_products per_page
query on my homepage but I want to limit to 12 products so that I can add the infinity scroll option.
<?php get_header(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => -1
);
$wp_query = new WP_Query($args);
if (isset($_GET['all']))
{
?>
<?php do_action('woocommerce_archive_description'); ?>
<?php if (have_posts()) : ?>
<?php
// I don't want the sorting anymore
//do_action('woocommerce_before_shop_loop');
?>
<ul class = "products-list">
<?php while (have_posts()) : the_post(); ?>
<?php woocommerce_get_template_part('content', 'product'); ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php
/* woocommerce pagination */
do_action('woocommerce_after_shop_loop');
?>
<?php elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
<?php woocommerce_get_template('loop/no-products-found.php'); ?>
<?php endif; ?>
<?php else: ?>
{
// Code to display the product categories with thumbnails.
}
?>
<?php get_footer(); ?>
Would be great if someone can help me out with this. I need this template or a custom template you suggest to show the same pagination as the shop page (1,2,3) etc..
Thanks.

posts per page on a static page + pagination

Thanks for taking the time to check this out.
I've spend a couple days going over this. Lots of time on wp.org but i'm just not getting it. Should be an easy fix I'm sure. No matter what i try, i can't limit the number of posts per page nor get any pagination to display. This is my most recent attempt (maybe not my best attempt). The page just shows all the posts OR all the recent ones or something (not my site). Once i can at least get the page to limit the posts, then i'll tackle the pagination. Also, setting the posts per page in the WP dashboard does nothing...and never did. That's why i'm trying to code something myself. Why can't i limit the number of posts per page? Would i put the pagination where i currently have it? Is this a total mess, lol?
Thanks again,
Dave (code below)
<?php /* Template Name: Stories */ ?>
<?php
get_header(); ?>
<!-- *************************************** -->
<div class="custom_two_third">
<?php
// The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
}
// pagination
next_posts_link();
previous_posts_link();
} else {
get_template_part( 'template-parts/content', 'none' );
}
/* Restore original Post Data */
wp_reset_postdata();
?>
<div class="clear"></div>
</div><!-- custom_two_third -->
<?php
get_sidebar();
get_footer();
?>
Try using post_type
$args_articles =array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'order' => 'desc',
'paged' => $paged
);
// the query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news_eve_args = array( 'post_type' => 'news_events', 'posts_per_page' => 4, 'paged' => $paged);
$wp_query = new WP_Query( $news_eve_args );
if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php the_title(); echo "<br/>"; ?>
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer',$wp_query->max_num_pages); ?>
<?php next_posts_link('Older »',$wp_query->max_num_pages); ?>
</nav>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no news or events at this time.', 'theme' ); ?></p>
<?php endif; ?>
Try this one, it will work in your case.
global $query_string;
query_posts("{$query_string}&posts_per_page=12");
while (have_posts()) {
the_post();
...
}

Wordpress single.php second query pagination

I created a single.php Wordpress template file to show standard WP Posts.
This is the truncated version of the code:
<?php
// First (Main Content) Loop
if (have_posts()) :
while (have_posts()):the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
<?php
// Second (Related Posts) Loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'category__in' => 3,
'posts_per_page'=> 4,
'paged' => $paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new wp_query($args);
if( $wp_query->have_posts() ) :
while ($wp_query->have_posts()) :
$wp_query->the_post();
?>
<div class="entry-content">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
echo '<div id="navigation">'.get_next_posts_link('Load More').'</div>';
endif;
}
$wp_query = null;
$wp_query = $temp;
?>
The 4 related posts are generated as expected and the get_next_posts_link generates a link to /post-name/page/2.
I'm using Paul Irish's Infinite Scroll plugin to load page/2's posts into the container (Which is working fine on the archive- and index.php pages on the site).
Unfortunately when you click get_next_posts_link the same 4 posts are appended to the container.
Any ideas how to get this to work?
I've tried a lot of other solutions on StackOverflow but so far none have worked.
Any help appreciated, cheers.

wp pagenavi for pagination ,too many pages for number of posts

is it possible to have wp-pagenavi set up so that the number of numbers in the pagination is relative to the number of posts.
At the moment , even there is only 3 or 4 posts in a category it still gives me the option to go to lots of old pages , which obviously just show up empty.
Here is an example, there are only 4 posts in this category...
http://limerickfc.hailstormcommerce.com/cms/?page_id=2466
Here is the code Im using for the loop.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("posts_per_page=12&paged=$paged");
$archive_query = new WP_Query('cat=14&showposts=12&paged=' . $paged);
...
if (have_posts()) :
while ($archive_query->have_posts()) : $archive_query->the_post();
...do stuff
?>
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
<?php endif; ?>
That's not how I would use WP-Pagenavi, you don't need to include the pagination part of the custom query, I think that is the source of your problem.
$archive_query = new WP_Query('cat=14&showposts=12');
Incidentally, showposts has been deprecated in favour or posts_per_page.
My solution...
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>
<?php query_posts('cat=14&posts_per_page=3'."&paged=$paged"); ?>
<?php
$id = get_the_ID();
$count = 0;
if (have_posts()) : ?>
<?php while ( have_posts() ) : the_post();
........
<?php endwhile; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query();?>
<?php endif; ?>

Categories