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 Answers
Reset to default 2You'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
版权声明:本文标题:php - Parse error: syntax error, unexpected '}' surrounding a while 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741702547a2393375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
endwhile
. – vancoder Commented Mar 4, 2021 at 22:17if() {}
before you close thewhile() : endwhile;
, which is where the error is coming from. – Pat J Commented Mar 5, 2021 at 2:36