My post/pages sitemap only shows 1 image

#39512
  • Resolved Dino Gomez
    Rank Math pro

    Hi,

    When I create the sitemap, RM only displays the posts or pages as if they only contained 1 image, I have posts with over 100 images in them, but the sitemap only shows 1. Please help!!

    Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • It’s the same for me! Before, my sitemap was chock full of images on every page.

    Todd
    Rank Math free

    Hi Dino,

    Thanks a lot for your query and we are so sorry about the trouble this must have caused.

    Are you using any page builder plugin to add those images?

    If so, could you please tell us which one? We tried to find out but seems like you have blocked certain countries from accessing your website.

    Dino Gomez
    Rank Math pro

    Thank you for your reply!

    I’m using Avada Theme, which uses Fusion Builder.

    Hello,

    The Fusion Builder plugin adds the images in the shortcode and that’s the reason Rank Math is not able to read it. Please contact the Fusion Builder team and ask them to add the compatibility code using the following filter just like they are doing for other SEO plugins: https://rankmath.com/kb/filters-hooks-api-developer/#remove-change-images

    For now to fix this on your site, please add following code in your theme’s functions.php file:

    
    add_filter( 'rank_math/sitemap/urlimages', function( $images, $post_id ){
        $post    = get_post( $post_id );
            $content = $post->post_content;
    
            // For images from fusion_imageframe shortcode.
            if ( preg_match_all( '/\[fusion_imageframe(.+?)?\](?:(.+?)?\[\/fusion_imageframe\])?/', $content, $matches ) ) {
    
                foreach ( $matches[0] as $image_frame ) {
                    $src = '';
    
                    if ( false === strpos( $image_frame, '<img' ) && $image_frame ) {
    
                        $pattern = get_shortcode_regex();
                        $matches = [];
                        preg_match( "/$pattern/s", $image_frame, $matches );
                        $src = $matches[5];
                    } else {
                        preg_match( '/(src=["\'](.*?)["\'])/', $image_frame, $src );
    
                        if ( array_key_exists( '2', $src ) ) {
                            $src = $src[2];
                        }
                    }
    
                    if ( ! in_array( $src, $images, true ) ) {
                        $images[] = [
                            'src' => $src,
                        ];
                    }
                }
            }
    
            // For images from newer structure of gallery element.
            if ( preg_match_all( '/\[fusion_gallery_image(.+?)?\](?:(.+?)?\[\/fusion_gallery_image\])?/', $content, $matches ) ) {
                foreach ( $matches[0] as $item ) {
                    $atts = shortcode_parse_atts( $item );
                    if ( isset( $atts['image'] ) && ! empty( $atts['image'] ) ) {
                        $images[] = [
                            'src' => $atts['image'],
                        ];
                    } elseif ( isset( $atts['image_id'] ) && ! empty( $atts['image_id'] ) ) {
                        $images[] = [
                            'src' => wp_get_attachment_url( $atts['image_id'] ),
                        ];
                    }
                }
            }
    
            // For images from older structure fusion_gallery shortcode.
            if ( preg_match_all( '/\[fusion_gallery(.+?)?\](?:(.+?)?\[\/fusion_gallery\])?/', $content, $matches ) ) {
                foreach ( $matches[0] as $image_gallery ) {
                    $atts = shortcode_parse_atts( $image_gallery );
                    if ( isset( $atts['image_ids'] ) && ! empty( $atts['image_ids'] ) ) {
                        $image_ids = explode( ',', $atts['image_ids'] );
                        foreach ( $image_ids as $image_id ) {
                            $images[] = [
                                'src' => wp_get_attachment_url( $image_id ),
                            ];
                        }
                    }
                }
            }
    
            // For images from fusion_image_before_after shortcode.
            if ( preg_match_all( '/\[fusion_image_before_after(.+?)?\](?:(.+?)?\[\/fusion_image_before_after\])?/', $content, $matches ) ) {
                foreach ( $matches[0] as $item ) {
                    $atts = shortcode_parse_atts( $item );
                    if ( isset( $atts['before_image'] ) && ! empty( $atts['before_image'] ) ) {
                        $images[] = [
                            'src' => $atts['before_image'],
                        ];
                    }
                    if ( isset( $atts['after_image'] ) && ! empty( $atts['after_image'] ) ) {
                        $images[] = [
                            'src' => $atts['after_image'],
                        ];
                    }
                }
            }
    
            return $images;
    }, 10, 2 );
    

    I hope that helps. Please let us know if you need any further assistance.

    Thank you.

    Hi Pratik, I have the same problem but I use the Enfold theme. Using Yoast I had to insert the code below in the function.php file.
    Without this code with the Enfold theme the sitemap had some but not all images.
    With RankMat and the Enfold theme, the images in the sitemap were zero (1 image if I add the highlighted image).

    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);

    Dino Gomez
    Rank Math pro

    It did worked! Thank you!!

    Todd
    Rank Math free

    Hi Dino,

    We are so glad to be of help. We are always here if you need us in the future.

    Hey, if it isn’t too much to ask for – would you mind leaving us a review here?

    https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post

    It only takes a couple of minutes but helps us tremendously.

    It would mean so much to us.

    Giuseppe – do you mind opening a new ticket so we can help you better?

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘My post/pages sitemap only shows 1 image’ is closed to new replies.