0

I have a problem I am not able to solve. I have a custom theme with some custom post types and I need to display an archive for all CPT somewhere in my site. After some reading, I decided that the best solution would be to create a new Page named Blog (slug is 'blog') and assign that page as the blog page in Settings->reading->page for posts. That gives me an URL which is http://localhost/traveling/blog/ that uses index.php file to show the query.

Inside index.php i have this:

<?php
get_header();
?>
    <section id="primary" class="content-area">
        <main id="main" class="site-main">

            <?php
            get_template_part( 'template-parts/loop', 'blog' );
            ?>

        </main><!-- #main -->
    </section><!-- #primary -->

<?php
get_footer();

As called above, I have the file template-parts/loop-blog.php, which contains only this:

<?php
// The Query
$args = array(
    'post_type'         => array(
        'post',
        'travel_tip',
        'city_guide',
        'travel_diary'
    ),
    'orderby'           => 'date',
    'order'             => 'desc',
    'posts_per_page'    => 1,
    'paged'             => ( get_query_var('paged') ) ? get_query_var('paged') : 1
);
$wp_query = new WP_Query( $args );
 
// The Loop
if ( $wp_query->have_posts() ) {
    
    echo '<div class="stackedposts">';

        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();
            get_template_part( 'template-parts/content', 'preview' );
        }

    echo '</div><!-- .stackedposts -->';
    
}

// Pagination
travelblog_the_posts_navigation();

// Restore original Post Data
wp_reset_postdata();

This calls the file template-parts/content-preview.php which has all the markup to display a single post.

This is all working as expected and the first post displays in http://localhost/traveling/blog/. Pagination links are also showing up normally. If I raise posts_per_page, I can see the posts normally too.

However, the problem is that when clicking on a link from the navigation, to go to, for example, http://localhost/traveling/blog/page/2/, it returns a 404 error instead. My guess is that I am missing something that's interfering with the inner Blog page's pagination, but I am not able to find a solution.

I have a similar setup with all the individual CPT archive pages using archive-travel_diary.php for example, which are working perfectly and correctly displaying the posts according to the current page.

--

EDIT

Just did a quick test...

I completely deleted the 404.php file and the posts are displaying properly in the desired page, but the page title returns Not Found.

Maybe the problem is not in the code from my previous post...

0

1 Answer 1

0

As always, at the very moment you give up, the solution comes before your eyes...

I was using a static page as the posts page, set up in Settings->reading->page for posts. Removing this has solved the problem. Now I have setup a page-blog.php template, which I have selected as a template for the Blog page and everything works as expected.

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.