-1

I'm having trouble with trying to make a blog listing page, I have a page in Wordpress called Blog. Within this is some written content and that's it. I'm using the following query to make it output the content of that page:

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    <h2>Clever Thinking</h2>
    <p><?php the_content(); ?> </p>
<?php endwhile; ?>

This all works fine. Below that, I'm trying to create a query to all my general posts, with a pagination, displays four posts per page.

<!-- Fetch blog posts -->
        <?php 
            // The query for 4 posts
            $query = new WP_Query( 'posts_per_page=4' );

            // Output the results of the query
            if ( $query->have_posts() ) : 


            while ( $query->have_posts() ) : $the_query->the_post(); ?>
                <h2><?php the_title(); ?></h2>

            <?php endwhile; ?>
            <!-- end of the loop -->

            <!-- pagination here -->

            <?php wp_reset_postdata();

            // If no results appear
            else : ?>
                <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
            <?php endif; ?>

But this part is apparently not correct as I do not get anything, not even the posts error message, so the function is not even firing? I can't work out why this query would not work.

1
  • 2
    $the_query->the_post(); should be $query->the_post(); Commented Sep 4, 2014 at 8:12

1 Answer 1

1

Use query link this. You are checking posts in $the_query. but you have used $query.

<?php 

    $custom_query = new WP_Query( 'posts_per_page=4' );

    if ( $custom_query->have_posts() ) :

      while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>

          <h2><?php the_title(); ?></h2>

      <?php endwhile;

    else :

        get_template_part( 'content', 'none' );

    endif;

    wp_reset_postdata();

?>
2
  • Thanks, I feel really stupid now. But at least it wasn't a problem with the logic I guess!
    – Francesca
    Commented Sep 4, 2014 at 8:13
  • Yes, you were right. :)
    – Robert hue
    Commented Sep 4, 2014 at 8:17

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.