1

I have problem with custom gallery shortcode and missing first image in each gallery block. For example: [gallery ids="1,2,3,4,5"] show only 2,3,4,5 pictures.

This custom shortcode is part of wordpress plugin which make custom feed. It works correctly on my second site with same plugins and theme (tested on localhost). I've tried to update whole wordpress installation, disabled all plugins with no success. So the problem is somewhere in db or with server? I'm do not have any idea how debug this strange behavior and need some advice please.

Custom gallery shortcode:

        add_shortcode('gallery', 'gallery_shortcode');
        add_filter( 'post_gallery', 'yturbo_gallery', 9999999, 2 );
        $content = do_shortcode($content);
        $content = yturbo_do_gallery($content);

        $ytad4meta = get_post_meta($post->ID, 'ytad4meta', true);
        $ytad5meta = get_post_meta($post->ID, 'ytad5meta', true);

    function yturbo_gallery( $output, $attr ) {

    $yturbo_options = get_option('yturbo_options');
    if ( ! is_feed($yturbo_options['ytrssname']) ) {return;}

    $post = get_post();

    static $instance = 0;
    $instance++;

    if ( ! empty( $attr['ids'] ) ) {
        // 'ids' is explicitly ordered, unless you specify otherwise.
        if ( empty( $attr['orderby'] ) ) {
            $attr['orderby'] = 'post__in';
        }
        $attr['include'] = $attr['ids'];
    }

    $html5 = current_theme_supports( 'html5', 'gallery' );
    $atts = shortcode_atts( array(
        'order'      => 'ASC',
        'orderby'    => 'menu_order ID',
        'id'         => $post ? $post->ID : 0,
        'itemtag'    => $html5 ? 'figure'     : 'dl',
        'icontag'    => $html5 ? 'div'        : 'dt',
        'captiontag' => $html5 ? 'figcaption' : 'dd',
        'columns'    => 3,
        'size'       => 'thumbnail',
        'include'    => '',
        'exclude'    => '',
        'link'       => ''
    ), $attr, 'gallery' );

    $id = intval( $atts['id'] );

    $atts['include'] = str_replace(array("»","″"), "", $atts['include']);
    $atts['orderby'] = str_replace(array("»","″"), "", $atts['orderby']);
    $atts['order'] = str_replace(array("»","″"), "", $atts['order']);
    $atts['exclude'] = str_replace(array("»","″"), "", $atts['exclude']);

    if ( ! empty( $atts['include'] ) ) {
        $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'],'offset' => 0 ) );

        $attachments = array();
        foreach ( $_attachments as $key => $val ) {
            $attachments[$val->ID] = $_attachments[$key];
        }

    } elseif ( ! empty( $atts['exclude'] ) ) {
        $attachments = get_children( array( 'post_parent' => $id, 'exclude' => $atts['exclude'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'],'offset' => 0 ) );
    } else {
        $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'],'offset' => 0 ) );
    }

    if ( empty( $attachments ) ) {
        return '';
    }

    $output = PHP_EOL.'<div data-block="gallery">'.PHP_EOL;

        foreach ( $attachments as $id => $attachment ) {
            $output .= '<img src="'.wp_get_attachment_url($id) . '"/>'.PHP_EOL;
        }

    $output .= '</div>'.PHP_EOL;

    return $output;
}

I've tried to edit plugin code as it suggested here for gallery shortcode, but in this case both installations show all images except first.

    function yturbo_gallery( $output, $attr ) {

    $yturbo_options = get_option('yturbo_options');
    if ( ! is_feed($yturbo_options['ytrssname']) ) {return;}
    
    
    $output = "<div data-block=\"gallery\">";
    $posts = get_posts(array('include' => $attr['ids'],'post_type' => 'attachment'));

    foreach($posts as $imagePost){
        $output .= "<img src='".wp_get_attachment_image_src($imagePost->ID, 'full')[0]."'>";
    }

    $output .= "</div>";

    return $output; 
}

I've tested loops with 'offset' => 0 with no results.

2
  • Have you checked that the IDs you've provided are actually attachments? Jan 19, 2021 at 20:49
  • loop has 'post_type' => 'attachment', so I think they are actually attachments
    – Nar
    Jan 19, 2021 at 22:06

1 Answer 1

0

The problem was in additional symbols &#8221 in $attr['ids'] array. So I've changed:

$atts['include'] = str_replace(array("&#187;","&#8243;","&#8221;"), "", $atts['include']);
$atts['orderby'] = str_replace(array("&#187;","&#8243;","&#8221;"), "", $atts['orderby']);
$atts['order'] = str_replace(array("&#187;","&#8243;","&#8221;"), "", $atts['order']);
$atts['exclude'] = str_replace(array("&#187;","&#8243;","&#8221;"), "", $atts['exclude']);

And now everything ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.