$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Parse error: syntax error, unexpected '}' surrounding a while|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Parse error: syntax error, unexpected '}' surrounding a while

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far