admin管理员组

文章数量:1302328

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>

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>
Share Improve this question asked Mar 4, 2021 at 21:49 Laura SageLaura Sage 2255 silver badges11 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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;
}

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.

本文标签: phpParse error syntax errorunexpected 3939 surrounding a while