I have a big problem with my custom post pagination on home.php page. It shows the first 4 post but when I go to the second page I get a page with only the header and footer but no news content and no sidebar.
In the chrome console I get an error Failed to load resource: the server responded with a status of 404 (Not Found)
Below is my code. I'm creating a theme from scratch. It looks like wordpress would loose the content.php on second page or some other file. What could be the problem?
function.php
function pagination($pages = '', $range = 4)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« First</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Previous</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next ›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last »</a>";
echo "</div>\n";
}
}
home.php
<div class="main-content">
<div class="container">
<div class="sidebar col-xl-4">
<?php get_template_part( 'templates/sidebar' ); ?>
</div>
<div class="news-content col-xl-8">
<h1>News</h1>
<?php
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
$args = array(
'post_type' => 'news',
'posts_per_page' => 4,
'paged' => $paged
);
$custom_query = new WP_Query( $args );
while($custom_query->have_posts()) :
$custom_query->the_post();
?>
<?php if(has_post_thumbnail()) :?>
<div class="news-wrapper col-xl-6">
<div class="news-date">
<?php the_time('j.m.Y'); ?>
</div><!-- .news-date -->
<div class="news-img">
<?php the_post_thumbnail(); ?>
</div><!-- .news-img -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_excerpt(25); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
read more
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php else :?>
<div class="news-wrapper col-xl-6">
<div class="news-date">
<?php the_time('j.m.Y'); ?>
</div><!-- .news-date -->
<a class="news-title" href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a><!-- .news-title -->
<div class="news-text">
<?php echo short_news_content(70); ?>
</div>
<a href="<?php the_permalink() ?>" class="btn">
read more
</a><!-- .btn -->
</div><!-- .news-wrapper -->
<?php
endif;
endwhile;
?>
<?php wp_reset_query(); ?>
<nav class="pagination col-xl-12">
<?php if (function_exists("pagination")) {pagination($custom_query->max_num_pages);} ?>
</nav>
</div><!-- .news-content -->
</div><!-- .container -->
</div><!-- .main-content -->
EDIT
I was digging for the reason why the pagination is giving a 404 error. I have deleted my home.php file for now and I was left with the index.php witch is showing only the header and footer. And this is exactly what I see on my CPTs second page. I have added later on the else no posts
to check my suspicions.
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
else:
echo '<p>no post</p>';
endif;
?>
so for some reason on the index page wordpress is not able to show my CPT. Any idea how to fix this?
page
instead ofpaged
. See codex.wordpress.org/Class_Reference/WP_Querypre_get_posts
to alter the main query rather than create a custom query.