• Resolved wp_kc

    (@wp_kc)


    Thank you for your work on this excellent plugin.

    I work on a web site that uses the Advanced Custom Fields plugin to embed images within a post with a different format than the standard WordPress featured image. The custom field option of your plugin was not retrieving the image URL when using the custom field name from ACF. Below is a code sample that detects if ACF is active, and the uses ACF’s function to retrieve the image URL.

    Change line 508 of related-posts-thumbnails.php from…
    $url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );

    to…

    if ( function_exists( 'acf_shortcode' ) ) {
       $url = $basic_url = get_field( get_option( 'relpoststh_customfield', $this->custom_field ), $post->ID );
    } else {
       $url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );
    }

    The specified custom field in ACF needs to be set to return Image URL for this to work (default is Image Object). Since the above code is executed in a loop, it could probably be optimized by calling function_exists( ‘acf_shortcode’ ) and get_option( ‘relpoststh_customfield’, $this->custom_field ) before the loop.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Asad Shahbaz

    (@artisticasad)

    Hi @wp_kc,

    Thank you for your suggestion.
    Related Posts Thumbnails already supports custom fields along with ACF but if you are unable to retrieve image URL then there are a few things to be checked:

    1. You have to select ‘Custom Field’ for the option ‘Thumbnails Source’ in ‘General Display Options’.
    2. The ACF custom field type should be text to provide image URL.
    3. You have to provide ACF field name NOT field label in ‘Custom field name’ under the settings ‘Thumbnails Source’.
    Thread Starter wp_kc

    (@wp_kc)

    I don’t know why it is not working then. When I do everything you recommended, the image urls show up as “https://234” or similar, like it is receiving an ID. However, if I change your code to…

    
    $acf_exists = function_exists( 'acf_shortcode' );
    
            foreach ( $posts as $post ) {
                $image = '';
                $url   = '';
                $alt   = '';
                if ( $thsource == 'custom-field' ) {
                    $debug .= 'Using custom field;';
                    //$url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );
    
    if ( $acf_exists ) {
       $url = $basic_url = get_field( get_option( 'relpoststh_customfield', $this->custom_field ), $post->ID );
    } else {
       $url = $basic_url = get_post_meta( $post->ID, get_option( 'relpoststh_customfield', $this->custom_field ), true );
    

    …then everything works perfectly. I’m guessing that ACF does not store custom field data in the postmeta table using the specified ACF field name, but instead does some sort of name mangling that get_field() undoes.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘New Feature: Integration with Advanced Custom Fields Plugin’ is closed to new replies.