0

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)."'>&laquo; First</a>";
     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; 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 &rsaquo;</a>";
     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</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?

5
  • for static home page use page instead of paged. See codex.wordpress.org/Class_Reference/WP_Query Commented May 30, 2018 at 8:30
  • I have updated my query at home.php but still got this problem.
    – Dariusz
    Commented May 30, 2018 at 8:39
  • try nav tag before wp_reset_query and after endwhile Commented May 30, 2018 at 8:57
  • I moved the nav tag as you suggested but it didn't work
    – Dariusz
    Commented May 30, 2018 at 9:20
  • You can't paginate a custom query beyond the number of pages in the main query on any kind of archive page. Use pre_get_posts to alter the main query rather than create a custom query.
    – Milo
    Commented Jun 5, 2018 at 0:30

1 Answer 1

0

Thanks to Milo's comment I could find a solution.

In my home.php I have deleted the custom query and inserted:

<div class="news-content col-12 col-xl-8">
      <div class="row">
        <h1>News</h1>
      </div><!-- .row -->

      <div class="row">
          <?php

            if ( have_posts() ) : while ( have_posts() ) : the_post();

              if(has_post_thumbnail()) :?>

                <div class="news-wrapper col-12 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">
                    czytaj dalej
                  </a><!-- .btn -->
                </div><!-- .news-wrapper -->

              <?php else :?>

                <div class="news-wrapper col-12 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">
                    czytaj dalej
                  </a><!-- .btn -->
                </div><!-- .news-wrapper -->

              <?php
                endif;
                endwhile;
                endif;
              ?>
      </div><!-- .row -->
      <div class="row">
        <?php the_posts_pagination( array( 'mid_size'  => 2 ) ); ?>
      </div>
    </div><!-- .news-content -->
  </div><!-- .container -->
</div><!-- .main-content -->

and in my functions.php file I have added:

add_action( 'pre_get_posts', 'custom_post_on_homepage' );

function custom_post_on_homepage( $query ) {

 if ( is_home() && $query->is_main_query() )
  {
    $query->set('post_type', 'news');
    $query->set( 'posts_per_page', '4' );
  }
 }

Hope this helps someone in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.