自定义提要的图库快捷代码中缺少图像

时间:2021-01-19 作者:Nar

我有自定义库短代码的问题,并且缺少每个库块中的第一个图像。例如:[库ID=“1,2,3,4,5”]仅显示2,3,4,5张图片。

这个自定义短代码是wordpress插件的一部分,该插件可以生成自定义提要。它使用相同的插件和主题(在localhost上测试)在我的第二个站点上正常工作。我试图更新整个wordpress安装,禁用了所有插件,但没有成功。那么问题出在db或服务器的某个地方?我不知道如何调试这个奇怪的行为,需要一些建议。

自定义库快捷码:

        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;
}
我曾尝试按照这里为gallery shortcode建议的那样编辑插件代码,但在这种情况下,两个安装都会显示除第一个以外的所有图像。

    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; 
}
我已经用\'offset\' => 0 没有结果。

1 个回复
SO网友:Nar

问题出在其他符号上&#8221 在里面$attr[\'ids\'] 大堆所以我改变了:

$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\']);
现在一切都好了。

相关推荐

是否使用插件覆盖主题中的WooCommerce loop-start.php?

“我的主题”有一个覆盖内容/主题/主题名/woocommerce/循环/loop-start.php.我想使用插件覆盖此文件。我知道可以使用子主题来完成,但我想使用插件(因为我在1个插件中捆绑了许多其他修改)。我在堆栈交换上发现了其他一些类似的问题,但无法找到有效的解决方案。以下是我的尝试:使用theme_file_path:add_filter( \'theme_file_path\', \'override_woocommerce_loop_template\', 9999); fu