0

I'm using the following code to get posts from a category in a page. This category uses ACF for some fields. However, when I try to output everything, it shows everything but the_content. For better understanding, here's my code:

<div class="content ">
    <?php if (have_posts()) : ?>
    <h1 class="pricing">Prices</h1>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>

    <!-- START LIST -->
    <?php
    global $post;
    $my_query = new WP_Query('showposts=9999&category_name=my-category');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;
    $category = get_the_category();
    $short = get_field('short_excerpt'); // and more custom fields
    ?>

    <!-- START PRICE CHART -->
    <div class="pricechart">

        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
        <p class="subtitle"><?php echo $short;?></p>

        <div class="leftexcerpt">

        <?php
        if ($cap) {echo '<div class="price_row">
        <div class="price_row_left">Max Guests</div>
        <div class="price_row_right">'.$cap.'</div>
        </div>';

        } elseif ($capt) {echo '<div class="price_row">
        <div class="price_row_left">Max Guests</div>
        <div class="price_row_right">'.$capt.'</div>
        </div>';

        } else {} ?>

<!-- and some additional ACF content that displays properly -->
        </div>

        <!-- THE THUMB COLUMN -->
        <div class="thzone">
        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('archi') ?></a>
        </div>
    </div><!-- /pricechart -->
<?php endwhile;?>

<?php endif;?>

</div><!-- /content -->

With this code, I can display everything, from title to all the posts and their ACF fields, permalinks, thumbnails, no problem at all. However, it doesn't display the main content

I assumed it had to be something really stupid, but I spent 4 hours so far and cannot figure it out so coming here as last resource. I also thought ACF could be the culprit, but it doesn't seem to be the reason since everything (included ACF) is displaying properly, however I mention this just in case it is of help. As you'll probably see, I'm not PHP savvy, just trying to learn as I go.

  • 1
    The first loop is incomplete. You're missing while have_posts() and the_post() – gdaniel Feb 12 '15 at 22:42
  • ah, thought I could do it just as a single loop. I've used 2 loops with <?php if (have_posts()) : while (have_posts()) : the_post(); ?> earlier but thought it was wrong and had to use only one loop. Thank you @gdaniel ! – Ariel Feb 12 '15 at 22:54
  • You can have nested loops, but you should use wp_reset_query(); and wp_reset_postdata(); after each endif; to make sure the data is not mixed. – gdaniel Feb 12 '15 at 22:58
  • wp_reset_query should only be used if you're using query_posts, which you should never use – Tom J Nowell Feb 12 '15 at 22:59
  • Also, indenting ftw, you should pick a coding standard and stick to it, it makes your code easier to read, and questions easier to answer! – Tom J Nowell Feb 12 '15 at 22:59
0

You never start the main loop:

<?php if (have_posts()) : ?>
    <h1 class="pricing">Prices</h1>
    <h2><?php the_title(); ?></h2>
    <?php the_content(); ?>

You check if there are posts in the main query, but you never start the loop, and jump straight to showing the title and content, you need to move everything into a main post loop. Without a post loop, you can't do things with posts, even if it's a loop with a single post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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