admin管理员组文章数量:1130349
I have added a custom taxonomy image through ACF.
My code is as follows.
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
If a post does not have an items in the taxonoimy selected, I get the following error.
Warning
Invalid argument supplied for foreach() in /home/webspera/public_html/SVCTA/wp-content/themes/pro-child/taxonomy_directory_category-weddings.php
on line
168
How can I have this just hide the field if nothing has been selected?
I am not a porgrammer so thanks in advance for the help.
I have added a custom taxonomy image through ACF.
My code is as follows.
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
If a post does not have an items in the taxonoimy selected, I get the following error.
Warning
Invalid argument supplied for foreach() in /home/webspera/public_html/SVCTA/wp-content/themes/pro-child/taxonomy_directory_category-weddings.php
on line
168
How can I have this just hide the field if nothing has been selected?
I am not a porgrammer so thanks in advance for the help.
Share Improve this question asked Nov 20, 2018 at 6:28 Micah KMicah K 155 bronze badges1 Answer
Reset to default 0Seems the your post doesn't attach any taxonomy. So just try to replace your code with this:
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
if ( $terms && ! is_wp_error( $terms ) ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
}
?>
I have added a custom taxonomy image through ACF.
My code is as follows.
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
If a post does not have an items in the taxonoimy selected, I get the following error.
Warning
Invalid argument supplied for foreach() in /home/webspera/public_html/SVCTA/wp-content/themes/pro-child/taxonomy_directory_category-weddings.php
on line
168
How can I have this just hide the field if nothing has been selected?
I am not a porgrammer so thanks in advance for the help.
I have added a custom taxonomy image through ACF.
My code is as follows.
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
If a post does not have an items in the taxonoimy selected, I get the following error.
Warning
Invalid argument supplied for foreach() in /home/webspera/public_html/SVCTA/wp-content/themes/pro-child/taxonomy_directory_category-weddings.php
on line
168
How can I have this just hide the field if nothing has been selected?
I am not a porgrammer so thanks in advance for the help.
Share Improve this question asked Nov 20, 2018 at 6:28 Micah KMicah K 155 bronze badges1 Answer
Reset to default 0Seems the your post doesn't attach any taxonomy. So just try to replace your code with this:
<?php
global $post;
$tax = 'directory_features';
$terms = get_the_terms($post,$tax);
if ( $terms && ! is_wp_error( $terms ) ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('svcta_favorites_image',$term);
if( $term->count > 0 ) {
echo '<li>';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'"><br>';
echo '</li>';
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
}
?>
本文标签: ACF Fieldhide taxonomy title and image when no nothing selected in post
版权声明:本文标题:ACF Field, hide taxonomy title and image when no nothing selected in post 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749167629a2326455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论