Sitemap only shows 1 image with Enfold Theme

#40299
  • Resolved Giuseppe Mazzardi
    Rank Math free

    Hi,

    I use Enfold Theme,
    when I create the sitemap, RankMath only displays the posts or pages as if they only contained 1 image, I have pages with over 10 images, but the sitemap only shows 1.

    I have always used Yoast. With yoast the images in the sitemap were greater than 1 (but not all those present on the pages). By adding this string in the function.php file (which I attach below) the problem with Yoast is solved.

    But with RankMath instead I don’t know how to modify the file to see all the images.

    Here is the code for Enfold using Yoast:

    //add image to site maps
    function avia_extract_shortcode($elements, $content) {
    $container = array();
    if(!empty($elements)) {
    foreach ($elements as $key => $element)
    {
    preg_match_all($element[‘pattern’], $content, $shortcodes);
    foreach($shortcodes[0] as $shortcode)
    {
    switch ($element[‘source’]) {
    case ‘ids’:
    $src = ‘/ids=\\\'(\d+(,\d+)*)\\\’/’;
    break;
    case ‘src’:
    $src = ‘/attachment=\\\'(\d+)\\\’/’;
    break;
    case ‘sid’:
    $src = ‘/id=\\\'(\d+)\\\’/sim’;
    break;
    default:
    return;
    }
    
    $sid = array();
    
    preg_match_all($src, $shortcode, $id);
    
    if($src = ‘sid’) {
    foreach($id[1] as $key => $value) {
    $sid[] = $value;
    }
    
    $sid = implode(‘,’, $sid);
    $id[1] = $sid;
    }
    
    $container[] = $id[1];
    }
    }
    }
    
    if(!empty($container)) {
    foreach($container as $key => $value) {
    $container[$key] = explode(‘,’, $value);
    }
    }
    
    if (count($container) > 0) {
    $container = call_user_func_array(‘array_merge’, $container);
    }
    
    return $container;
    }
    
    function avia_filter_wpseo_sitemap_urlimages($images, $post_id)
    {
    $post = get_post($post_id);
    if (is_object($post)) {
    $content = $post->post_content;
    $elements = apply_filters(‘avf_add_elements_wpseo_sitemap’,
    array(
    ‘masonry’ => array(
    ‘pattern’ => ‘/\[av_masonry_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    ),
    ‘gallery’ => array(
    ‘pattern’ => ‘/\[av_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    ),
    ‘horizontal’ => array(
    ‘pattern’ => ‘/\[av_horizontal_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    )
    ), $elements, $post_id);
    
    $ids = avia_extract_shortcode($elements, $content);
    
    foreach ($ids as $id)
    {
    $title = get_the_title($id);
    $alt = get_post_meta($id, ‘_wp_attachment_image_alt’, true);
    $src = wp_get_attachment_url($id);
    $images[] = array(‘src’ => $src, ‘title’ => $title, ‘alt’ => $alt);
    }
    }
    
    return $images;
    }
    add_filter(‘wpseo_sitemap_urlimages’, ‘avia_filter_wpseo_sitemap_urlimages’, 10, 2);
    add_filter(‘avf_add_elements_wpseo_sitemap’, function($elements, $postid) {
    $image = array(
    ‘image’ => array(
    ‘pattern’ => ‘/\[av_image [^]]*]/’,
    ‘source’ => ‘src’
    ),
    ‘accordion’ => array(
    ‘pattern’ => ‘/\[av_slideshow_accordion(.+?)?\](?:(.+?)?\[\/av_slideshow_accordion\])?/sim’,
    ‘source’ => ‘sid’
    ),
    ‘slideshow’ => array(
    ‘pattern’ => ‘/\[av_slideshow(.+?)?\](?:(.+?)?\[\/av_slideshow\])?/sim’,
    ‘source’ => ‘sid’
    ),
    ‘slideshow_full’ => array(
    ‘pattern’ => ‘/\[av_slideshow_full(.+?)?\](?:(.+?)?\[\/av_slideshow_full\])?/sim’,
    ‘source’ => ‘sid’
    ),
    ‘slideshow_fullscreen’ => array(
    ‘pattern’ => ‘/\[av_fullscreen(.+?)?\](?:(.+?)?\[\/av_fullscreen\])?/sim’,
    ‘source’ => ‘sid’
    )
    );
    
    return array_merge($image, $elements);
    }, 10, 2);
Viewing 15 replies - 1 through 15 (of 17 total)
  • Hello,

    Thank you for contacting Rank Math today.

    Could you please test the following snippet and let me know how this goes:

    /**
     * Filter images to be included for the post in XML sitemap.
     *
     * @param array $images  Array of image items.
     * @param int   $post_id ID of the post.
     */
    add_filter( 'rank_math/sitemap/urlimages', function( $images, $post_id ){
    	$post = get_post($post_id);
    if (is_object($post)) {
    $content = $post->post_content;
    $elements = apply_filters(‘avf_add_elements_wpseo_sitemap’,
    array(
    ‘masonry’ => array(
    ‘pattern’ => ‘/\[av_masonry_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    ),
    ‘gallery’ => array(
    ‘pattern’ => ‘/\[av_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    ),
    ‘horizontal’ => array(
    ‘pattern’ => ‘/\[av_horizontal_gallery [^]]*]/’,
    ‘source’ => ‘ids’
    )
    ), $elements, $post_id);
    
    $ids = avia_extract_shortcode($elements, $content);
    
    foreach ($ids as $id)
    {
    $title = get_the_title($id);
    $alt = get_post_meta($id, ‘_wp_attachment_image_alt’, true);
    $src = wp_get_attachment_url($id);
    $images[] = array(‘src’ => $src, ‘title’ => $title, ‘alt’ => $alt);
    }
    }
    
    return $images;
    }, 10, 2);

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hi Michael, thank you…
    I added the code but I see this error message:

    Your changes to the PHP code have been canceled due to an error in line 21 of the wp-content / themes / enfold-child / features.php file. Please correct and try to save again.

    `syntax error, unexpected ‘[‘, expecting identifier (T_STRING)

    this is line 21

    ‘pattern’ => ‘/\[av_masonry_gallery [^]]*]/’,

    I tried to delete line 21 … the same problem signals me to this line:

    ‘pattern’ => ‘/\[av_gallery [^]]*]/’,

    And then this too …
    ‘pattern’ => ‘/\[av_horizontal_gallery [^]]*]/’,

    I don’t understand, also because the code seems identical to the old one

    Hello,

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WP logins in the designated Sensitive Data section.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    We really look forward to helping you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Ok Michael
    I added the login details.

    You will find a test page with 7 images inside, in various types: gallery, simple image, background image and highlighted image.

    the name of the page is:
    Image sitemap test page

    https://www.giuseppemazzardi.it/pagina-di-prova/

    I have many sites created with this theme, this is what I am least interested in indexing and on which I am testing.

    I am looking forward to resolving the problem so I can use RankMath on all my other sites.

    Thank you

    Hello,

    Thank you for contacting Rank Math today.

    I have checked your site and added an image which seems to reflect in the sitemap. You seem to be adding your images using shortcode which is probably affecting their reflection in the sitemap.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hi Michael
    unfortunately I didn’t understand.

    I checked the site now and the function.php page is empty and nothing has changed on the sitemap either.

    how should i do?

    Hello,

    Please add following code in your theme’s functions.php file:

    
    function avia_extract_shortcode($elements, $content) {
    	$container = array();
    	if ( ! empty( $elements ) ) {
    		foreach ( $elements as $key => $element ) {
    			preg_match_all($element['pattern'], $content, $shortcodes);
    			foreach ( $shortcodes[0] as $shortcode ) {
    				switch ($element['source']) {
    					case 'ids':
    						$src = '/ids=\\\'(\d+(,\d+)*)\\\'/';
    					break;
    					case 'src':
    						$src = '/attachment=\\\'(\d+)\\\'/';
    					break;
    					case 'sid':
    						$src = '/id=\\\'(\d+)\\\'/sim';
    					break;
    					default:
    					return;
    				}
    
    				$sid = array();
    
    				preg_match_all($src, $shortcode, $id);
    
    				if($src = 'sid') {
    					foreach($id[1] as $key => $value) {
    						$sid[] = $value;
    					}
    
    					$sid = implode(',', $sid);
    					$id[1] = $sid;
    				}
    
    				$container[] = $id[1];
    			}
    		}
    	}
    
    	if(!empty($container)) {
    		foreach($container as $key => $value) {
    			$container[$key] = explode(',', $value);
    		}
    	}
    
    	if (count($container) > 0) {
    		$container = call_user_func_array('array_merge', $container);
    	}
    
    	return $container;
    }
    
    function avia_filter_rank_math_sitemap_urlimages( $images, $post_id ) {
    	$post = get_post($post_id);
    	if (is_object($post)) {
    		$content = $post->post_content;
    		$elements = apply_filters('avf_add_elements_rank_math_sitemap',
    			array(
    					'masonry' => array(
    						'pattern' => '/\[av_masonry_gallery [^]]*]/',
    						'source' => 'ids'
    					),
    				'gallery' => array(
    					'pattern' => '/\[av_gallery [^]]*]/',
    					'source' => 'ids'
    				),
    				'horizontal' => array(
    					'pattern' => '/\[av_horizontal_gallery [^]]*]/',
    					'source' => 'ids'
    				)
    			),
    		$elements, $post_id );
    
    		$ids = avia_extract_shortcode($elements, $content);
    
    		foreach ( $ids as $id ) {
    			$title = get_the_title($id);
    			$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
    			$src = wp_get_attachment_url($id);
    			$images[] = array('src' => $src, 'title' => $title, 'alt' => $alt);
    		}
    	}
    
    	return $images;
    }
    add_filter('rank_math/sitemap/urlimages', 'avia_filter_rank_math_sitemap_urlimages', 10, 2);
    
    add_filter('avf_add_elements_rank_math_sitemap', function( $elements, $postid ) {
    	$image = array(
    			'image' => array(
    			'pattern' => '/\[av_image [^]]*]/',
    			'source' => 'src'
    		),
    		'accordion' => array(
    			'pattern' => '/\[av_slideshow_accordion(.+?)?\](?:(.+?)?\[\/av_slideshow_accordion\])?/sim',
    			'source' => 'sid'
    		),
    		'slideshow' => array(
    			'pattern' => '/\[av_slideshow(.+?)?\](?:(.+?)?\[\/av_slideshow\])?/sim',
    			'source' => 'sid'
    		),
    		'slideshow_full' => array(
    			'pattern' => '/\[av_slideshow_full(.+?)?\](?:(.+?)?\[\/av_slideshow_full\])?/sim',
    			'source' => 'sid'
    		),
    		'slideshow_fullscreen' => array(
    			'pattern' => '/\[av_fullscreen(.+?)?\](?:(.+?)?\[\/av_fullscreen\])?/sim',
    			'source' => 'sid'
    		)
    	);
    
    	return array_merge($image, $elements);
    }, 10, 2 );
    
    

    This should fix the issue.

    I hope that helps.

    Hi, Pratik
    Unfortunately it doesn’t work yet …

    there are no images in the sitemap:
    LINK PAGE SITEMAP

    Hello,

    I logged into your site and I cleared the cache. That seemed to have fixed the issue, the Sitemap is now showing the correct images count. Can you please confirm?

    Thank you.

    Hi Pratik
    perfect, I hadn’t thought about the cache. Now we’re almost there.

    I did all the tests image by image … I found only one case… where the background image is not counted in the sitemap.

    I modified my test page leaving only the background image. If you could also make this image appear it would be fantastic.

    https://www.giuseppemazzardi.it/pagina-di-prova/

    Otherwise everything is ok.

    Thank you so much

    Todd
    Rank Math free

    Hi,

    I am sorry but since BG images don’t work like regular images, it would be very hard to include them in the sitemap. The regular images are added via HTML and the BG via CSS.

    BG images serve no purpose in the Sitemap either so there isn’t anything you are missing out on either.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

Viewing 15 replies - 1 through 15 (of 17 total)

The ticket ‘Sitemap only shows 1 image with Enfold Theme’ is closed to new replies.