0

I'm just having trouble figuring out why I'm getting an error at the "}" after the "$attachment_image" line, just before "". I'm probably just too tired to see it.

<?php if($term) {
   $args = array(
      'post_type' => 'attachment',
      'post_mime_type' => 'image',
      'posts_per_page' => 1,
      'tax_query' => array(
         array(
            'taxonomy' => 'mediacat',
            'terms' => $term->term_id,
         )
      ),
      'orderby' => 'rand',
      'post_status' => 'inherit',
   );

   $loop = new WP_Query( $args );

   while ( $loop->have_posts() ) :

   $loop->the_post();

   $item = get_the_id();

   $attachment_image = wp_get_attachment_image_url( $item, 'square' );
} ?>

<figure class="cblNavMenu--icon__imgwrap">
   <div class="navimage" style="background-image: url('<?php if($term) { echo $attachment_image; } if($menu_link){ the_permalink(); } ?>');"></div>
</figure>

<?php if($term) {
   endwhile;
   wp_reset_postdata();
} ?>

</div>

<span class="cblNavMenu--label"><?php if($term) { if($cat_label) { echo $cat_label; } else { echo $current_term_name; } } if($menu_link){ if($cat_label) { echo $cat_label; } else { the_title(); } } ?></span>
3
  • 2
    You're missing an endwhile.
    – vancoder
    Commented Mar 4, 2021 at 22:17
  • It's not missing. It's part of the if($term) after <figure> Basically, the "while" statement is entirely surrounded by "if($term)" statements.
    – Laura Sage
    Commented Mar 4, 2021 at 23:25
  • 2
    OK, but you're closing the if() {} before you close the while() : endwhile;, which is where the error is coming from.
    – Pat J
    Commented Mar 5, 2021 at 2:36

2 Answers 2

2

You've wrapped the opening and closing of the while in separate if statements. This is the structure of your code.

if( $term ) {
    // etc.
   while ( $loop->have_posts() ) :
}

// etc.

if( $term ) {
   endwhile;
}

This is not valid PHP. You need to structure it like this:

if( $term ) {
    // etc.
   while ( $loop->have_posts() ) :

   // etc.

   endwhile;
}
1

UPDATE: Ah, there's a little more to it. I didn't see the endwhile further on in the code. To fix the code you would have to remove the } from the line after $attachment_image = wp_get_attachment_image_url( $item, 'square' ); and then replace the line <?php if ($term) { with <?php. That should work fine.

Old post:

After the line $attachment_image = wp_get_attachment_image_url( $item, 'square' );, place a line that reads endwhile;. This should solve your problem, which is caused by the while loop started earlier not being closed.

2
  • It's not missing. It's part of the if($term) after <figure> Basically, the "while" statement is entirely surrounded by "if($term)" statements. Tried your suggestion, but it just broke the code in a different spot.
    – Laura Sage
    Commented Mar 4, 2021 at 23:26
  • Ah, you're right. It's still a reasonably easy fix. The second if($term) is technically unnecessary, I've updated my answer.
    – Patriot
    Commented Mar 5, 2021 at 10:55

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.