最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - If ACF field is empty show different value

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I have a function setup in my functions.php file which I am trying to use to show a Advanced Custom Field with a fallback value if that field is empty. Here is what I have so far:

add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {

    $field = get_field( 'Type', $post_id ); // Get this post's ACF value
    $tent = 'tent'; 
    $value =   if( $field == true ){ echo $field;} else { echo $tent; };

    $args['icon'] = '/' . $value . '.png';
 
  return $args; }, 10, 2 );

In other words: If $field has a value then it should be use but if it is empty then it should default to $tent.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I have a function setup in my functions.php file which I am trying to use to show a Advanced Custom Field with a fallback value if that field is empty. Here is what I have so far:

add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {

    $field = get_field( 'Type', $post_id ); // Get this post's ACF value
    $tent = 'tent'; 
    $value =   if( $field == true ){ echo $field;} else { echo $tent; };

    $args['icon'] = 'https://www.muddycamper.com/wp-content/themes/oceanwp-child-theme-muddy-camper/images/markers/' . $value . '.png';
 
  return $args; }, 10, 2 );

In other words: If $field has a value then it should be use but if it is empty then it should default to $tent.

Share Improve this question asked Jan 26, 2022 at 0:48 moorewebxmoorewebx 571 gold badge2 silver badges6 bronze badges 2
  • 1 This is a question for ACF's support. Their documentation shows a way to check for an empty value — see the Check if value exists section. – Pat J Commented Jan 26, 2022 at 3:24
  • 1 What you're asking is a generic PHP/programming question which is better suited at Stack Overflow. But basically, $value = $field ? $field : $tent; would do the logic you described. – Sally CJ Commented Jan 26, 2022 at 4:32
Add a comment  | 

1 Answer 1

Reset to default 2

This is basically a php question.

add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {

    $field = get_field( 'Type', $post_id ); // Get this post's ACF value
    $tent = 'tent'; 
    if( !$field ){
        $field = $tent;
    }    
    $args['icon'] = 'https://www.muddycamper.com/wp-content/themes/oceanwp-child-theme-muddy-camper/images/markers/' . $field . '.png';
 
  return $args; }, 10, 2 );

You could do it your way as well, but it's extra coding:

add_filter( 'facetwp_map_marker_args', function( $args, $post_id ) {

    $field = get_field( 'Type', $post_id ); // Get this post's ACF value
    $tent = 'tent'; 
    if( $field ){
        $value = $field;
    } else {
        $value = $tent;
    }
    $args['icon'] = 'https://www.muddycamper.com/wp-content/themes/oceanwp-child-theme-muddy-camper/images/markers/' . $value . '.png';
 
  return $args; }, 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论