• Resolved jnui

    (@jnui)


    for some reason the developer of a site i am fixing used advanced custom fields for post images (instead of featured image field).

    this means that the blank image field will cause an error in structured data tester, especially for category archive pages.
    in the misc_functions.php file around line 190 I added the follow code

    		// Try something else...for premier stoneworks
    		//small edit by johnny niumata for when someone uses advanced custom fields for post image
    	if ( ! isset($image_url) || $image_url == '' ) {
    		$imageholder = get_field('news_image', get_the_ID());
    
    		$image_arr			= wp_get_attachment_image_src( $imageholder, 'full' );
    		$image_url			= $image_arr[0];
    		$image_width		= ($image_arr[1] > 696) ? $image_arr[1] : 696;
    		$image_height		= ($image_arr[1] > 696) ? $image_arr[2] : round((696/$image_arr[1]) * $image_arr[2]);
    	}	
Viewing 1 replies (of 1 total)
  • Plugin Author Hesham Zebida

    (@hishaman)

    Thanks for the code.

    Also, instead of editing the core plugin code, you can use the schema_wp_filter_media filter to override $media array values.

    Here is an example code that you can use in your theme’s functions.php file:

    add_filter( 'schema_wp_filter_media', 'schema_wp_override_media_with_image_attachement_id_post_meta_716235' );
    /**
     * Override media image with an attachment id from post meta
     *
     * @param array $media.
     * @since 1.5
     * @return array contains the image attributes
     */
    function schema_wp_override_media_with_image_attachement_id_post_meta_716235( $media ) {
    	
    	global $post;
    	
    	// Get attachment id from post meta
    	//
    	// Note: replace ATTACHMENT_ID_POST_META_KEY with the actual post meta key you use to save attachment id value
    	//
    	$attachment_id		= get_post_meta( $post->ID, 'ATTACHMENT_ID_POST_META_KEY', true);
    	
    	// Get image attributes
    	$image_attributes	= wp_get_attachment_image_src( $attachment_id, 'full' );
    	
    	// Get image other details
    	$image_url			= $image_attributes[0];
    	$image_width		= ( $image_attributes[1] > 696 ) ? $image_attributes[1] : 696; // Images should be at least 696 pixels wide
    	$image_height		= $image_attributes[2];
    	
    	// Build our media array
    	$media = array (
    		'@type' 	=> 'ImageObject',
    		'url' 		=> $image_url,
    		'width' 	=> $image_width,
    		'height' 	=> $image_height,
    		);
    		
    	return $media;
    }

    Here is a link to the gist. I hope this helps.

Viewing 1 replies (of 1 total)
  • The topic ‘for people using advanced custom fields for post images’ is closed to new replies.