Search code examples
wordpressinfinite-loop

Why does wp_query make my website loop endlessly?


I have a custom post type named " actualites ".

I want to wp_query the post data ( title and content for now ).

Here's the code I used :

<div class="row">

<div class="col-sm-8 blog-main">

    <?php

    // the query
    $query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>

    <?php if ( $the_query->have_posts() ) : ?>

        <!-- pagination here -->

        <!-- the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post();

                $the_query->the_post();
                echo '<div class="blog-post">';
                echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
                echo '<p class="the-content">' . the_content() . '</p>';

                echo '</div>';
         endwhile; ?>
        <!-- end of the loop -->

        <!-- pagination here -->

        <?php wp_reset_postdata(); ?>

    <?php else : ?>
        <p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
    <?php endif; ?>

</div>

<!-- /.blog-main -->

<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
    <?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
        <ul id="sidebar">
            <?php dynamic_sidebar( 'sidebar-1' ); ?>
        </ul>
    <?php endif; ?>
</div><!-- /.blog-sidebar -->

For some reason this makes my website loop endlessly. Nothing appears, not even the header or the footer. Why does this happen ? It's 100% the wp_query part.

I tried the standard query and had an "expected statement " that probably was the cause of my issue. I'm trying the alternate wp query code now, and I don't have any clue as to what doesn't work. Any help ? Thanks !


Solution

  • Your Code should be as follows. In your code $the_query variable is not defined. I think that's why it's not working. Also turn on WP DEBUG MODE when you are developing. So you will be able to see errors and warnings.

    <?php
    
    // the query
    $query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
    
    <?php if ( $query->have_posts() ) : ?>
    
        <!-- pagination here -->
    
        <!-- the loop -->
        <?php while ( $query->have_posts() ) : $query->the_post();
    
                echo '<div class="blog-post">';
                echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
                echo '<p class="the-content">' . the_content() . '</p>';
    
                echo '</div>';
         endwhile; ?>
        <!-- end of the loop -->
    
        <!-- pagination here -->
    
        <?php wp_reset_postdata(); ?>
    
    <?php else : ?>
        <p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
    <?php endif; ?>