• I’ll provide background below, but the TLDR is that I can’t find the right field/function to insert the post title at the <need_something_here> in the code below.

    if ( ! function_exists( 'before_fallback_tagline_text' ) ) {
        /**
         * Add description meta tag with excerpt text.
         *
         * @return void
         */
        function before_fallback_tagline_text( $value, $post_id, $field ) {
    
          // Check for banner image setting
           if ( empty( $value ) && 'tagline' === $field['name'] ) {
    
              // Retrieve thumbnail setting (As attachment ID)
              $fallback = get_post_meta( $post_id,<need_something_here>, true );
    
              // Substitute the fallback for the missing mobile attachment
              if ( ! empty( $fallback ) )
                 $value = $fallback;
           }
           return $value;
        }
    }
    add_filter( 'acf/load_value', 'before_fallback_tagline_text', 99, 3 );
    

    I’m working on a site that’s using ACF and Elementor. I need to create a hook that will insert a second field in between the chosen ACF and the fallback. (Hopefully I’m using those terms correctly.) In other words, in Elementor, you can say

    1. Display this ACF
    2. If it’s empty, display the designated fallback

    What I’d like the hook to do is change that to

    1. Display this ACF
    2. If it’s empty, display field_xyz
    3. If that’s empty too, display the designated fallback

    The code above works if ‘field_xyz’ is another ACF, but I can’t find the right thing to get it to use the post title as the second choice.

    I also have a variation of the code for images (pretty much the same code, just different names). Here again, it works fine if both the first choice and second choice are ACF, but I can’t find the right thing to use the post thumbnail (aka featured image) as the second choice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You will not find the post title in post meta, it’s one of the post object’s properties. You could use $fallback = get_the_title( $post_id );

    It’s not clear what aspect of a post’s featured image you’re after. There are a variety of post_thumbnail functions to get various related aspects like size, caption, URL, etc. If it’s the image’s URL you’re after, you can use get_the_post_thumbnail_url( $post_id, 'large');
    Replace ‘large’ with any defined size that’s available if desired, or pass an array of desired height and width dimensions instead.

    Thread Starter cweinhofer

    (@cweinhofer)

    Thanks for the help. I didn’t think about replacing everything after $fallback = If you can’t tell already, I don’t know a lot about PHP.

    $fallback = get_the_title( $post_id ); works great for the text side of things.

    But I’m still not finding the right thing for the featured image. I tried
    $fallback = get_the_post_thumbnail_url( $post_id, 'large');
    $fallback = get_the_post_thumbnail( $post_id, 'large');
    $fallback = the_post_thumbnail( $post_id, 'large');
    None of them work.

    What I need is for the code to just insert the image. Wish I could explain better. I do know that if I create an ACF image field called thumbnail_image then use $fallback = get_post_meta( $post_id,'thumbnail_image', true ); it works. But obviously that blunts the purpose of relying on the (already populated) featured image.

    Moderator bcworkz

    (@bcworkz)

    get_the_post_thumbnail( $post_id, 'large') will return the featured image’s complete <img> tag. If you were to echo out the returned tag from a front end template, the post’s featured image would be displayed. But I’m not sure if that will play well with an ACF field. For example, if the ACF filter is expecting a value for a text field, returning an <img> tag would likely display the raw HTML, not the related image itself.

    I don’t know ACF very well. If you’re having difficulty displaying a fallback featured image in an ACF field, I recommend asking in their dedicated support forum for advice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hook to Use Field Befor Fallback’ is closed to new replies.