• Resolved mian117

    (@mian117)


    Hi @héctor Cabrera,
    I want to make a fallback image request, if there is no featured image attached then it should look for the custom field and then display that image. is there any possibility for this, please let me know the file name, I tried some code changes in image.php and output.php, but it didn’t work. Thank You

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @mian117,

    Making changes to plugin’s code (or to a third-party theme) is not a good WordPress development practice: the next time you update the plugin WordPress will replace your modified plugin files with the stock ones automatically, undoing all of your plugin modifications.

    Instead, please use the built-in hooks that the plugin provides so you can make changes to it without having to modify its code – like so for example:

    <?php
    /**
     * Renders an alternative thumbnail from a custom field
     * if the post doesn't have a thumbnail.
     *
     * @author Hector Cabrera
     * @see    https://www.ads-software.com/support/topic/multiple-custom-fields-10/
     *
     * @param  string  $img_url  The default 'No thumbnail' image URL
     * @return string  $img_url  The (modified) 'No thumbnail' image URL
     */
    function wpp_thumbnail_fallback( $img_url, $post_id ) {
    
        // Post  doesn't have a thumbnail so let's fallback 
        // to the custom field one
        if ( ! has_post_thumbnail( $post_id ) ) {
            // Get the image URL from your custom field here
            // and then assign it to $img_url or return your URL
            //
            // eg. $img_url = get_post_meta( $post_id, 'MY_CUSTOM_FIELD_IMG_URL', true );
        }
    
        return $img_url;
    
    }
    add_filter( 'wpp_default_thumbnail_url', 'wpp_thumbnail_fallback', 10, 2 );

    If you have any other questions don’t hesitate to ask, ok?

    Thread Starter mian117

    (@mian117)

    I’m impressed with this fast response. Thank You very much.
    This solution solved the problem. I have tweaked it a bit to work as per my requirement for more than 1 custom field.

    /**
     * Renders an alternative thumbnail from a custom field
     * if the post doesn't have a thumbnail.
     *
     * @author Hector Cabrera
     * @see    https://www.ads-software.com/support/topic/multiple-custom-fields-10/
     *
     * @param  string  $img_url  The default 'No thumbnail' image URL
     * @return string  $img_url  The (modified) 'No thumbnail' image URL
     */
    function wpp_thumbnail_fallback( $img_url, $post_id ) {
    
        // Post  doesn't have a thumbnail so let's fallback 
        // to the custom field one
        if ( ! has_post_thumbnail( $post_id ) ) {
            // Get the image URL from your custom field here
            // and then assign it to $img_url or return your URL
    		if( get_field('graphic_910x512', $post_id) ){
              		$image_url_linkedin = get_field('graphic_910x512', $post_id);
    				$img_url =  $image_url_linkedin['url'];
    		}
    		else if( get_field('graphic_683x512', $post_id) ){
              		$image_url_linkedin = get_field('graphic_683x512', $post_id);
    				$img_url =  $image_url_linkedin['url'];
    		}
    		else if( get_field('xxl_graphic', $post_id) ){
              		$image_url_linkedin = get_field('xxl_graphic', $post_id);
    				$img_url =  $image_url_linkedin['url'];
    		}
    		
        }
    
        return $img_url;
    
    }
    add_filter( 'wpp_default_thumbnail_url', 'wpp_thumbnail_fallback', 10, 2 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple Custom Fields’ is closed to new replies.