自定义图库仅在将图像附加到帖子/页面时才起作用

时间:2014-06-09 作者:Ralphonz

我使用posts\\u gallery挂钩更改了我的gallery html,如下所示:How to customise the output of the WP image gallery shortcode from a plugin?

我已经从wordpress复制了最新的代码,并更改了$output变量来修改html。问题是,它只适用于附加到相关页面的图像。我想使用其他页面或已上传到图库中的图像。

这些库是使用媒体窗口用短代码生成的,如下所示:[库ID=“1,2,3,4,5”]。我已经尝试用include替换id,但这不起作用,我得到了一些自定义html输出以外的默认代码。

以下是我的自定义库输出:

function my_gallery_shortcode( $output = \'\', $atts, $content = false, $tag = false ) {
$return = $output; // fallback

// retrieve content of your own gallery function
$my_result = get_my_gallery_content( $atts );

// boolean false = empty, see http://php.net/empty
if( !empty( $my_result ) ) {
    $return = $my_result;
}

return $return;
}

add_filter( \'post_gallery\', \'my_gallery_shortcode\', 10, 4 );



function get_my_gallery_content ( $atts ) {

global $post;
// We\'re trusting author input, so let\'s at least make sure it looks like a valid orderby statement
if ( isset( $attr[\'orderby\'] ) ) {
    $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
    if ( !$attr[\'orderby\'] )
        unset( $attr[\'orderby\'] );
}

$html5 = current_theme_supports( \'html5\', \'gallery\' );
extract(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($id);
if ( \'RAND\' == $order )
    $orderby = \'none\';

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

    $attachments = array();
    foreach ( $_attachments as $key => $val ) {
        $attachments[$val->ID] = $_attachments[$key];
    }
} elseif ( !empty($exclude) ) {
    $attachments = get_children( array(\'post_parent\' => $id, \'exclude\' => $exclude, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
} else {
    $attachments = get_children( array(\'post_parent\' => $id, \'post_status\' => \'inherit\', \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => $order, \'orderby\' => $orderby) );
}

if ( empty($attachments) )
    return \'\';

if ( is_feed() ) {
    $output = "\\n";
    foreach ( $attachments as $att_id => $attachment )
        $output .= wp_get_attachment_link($att_id, $size, true) . "\\n";
    return $output;
}

$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( \'post\' );
if ( ! isset( $valid_tags[ $itemtag ] ) )
    $itemtag = \'dl\';
if ( ! isset( $valid_tags[ $captiontag ] ) )
    $captiontag = \'dd\';
if ( ! isset( $valid_tags[ $icontag ] ) )
    $icontag = \'dt\';

$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? \'right\' : \'left\';

$selector = "gallery-{$instance}";

$gallery_style = $gallery_div = \'\';

/**
 * Filter whether to print default gallery styles.
 *
 * @since 3.1.0
 *
 * @param bool $print Whether to print default gallery styles.
 *                    Defaults to false if the theme supports HTML5 galleries.
 *                    Otherwise, defaults to true.
 */
if ( apply_filters( \'use_default_gallery_style\', ! $html5 ) ) {
    $gallery_style = "
    <style type=\'text/css\'>
        #{$selector} {
            margin: auto;
        }
        #{$selector} .gallery-item {
            float: {$float};
            margin-top: 10px;
            text-align: center;
            width: {$itemwidth}%;
        }
        #{$selector} img {
            border: 2px solid #cfcfcf;
        }
        #{$selector} .gallery-caption {
            margin-left: 0;
        }
        /* see gallery_shortcode() in wp-includes/media.php */
    </style>\\n\\t\\t";
}

$size_class = sanitize_html_class( $size );
$gallery_div = "<div id=\'$selector-sausage\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class} images\'>";

/**
 * Filter the default gallery shortcode CSS styles.
 *
 * @since 2.5.0
 *
 * @param string $gallery_style Default gallery shortcode CSS styles.
 * @param string $gallery_div   Opening HTML div container for the gallery shortcode output.
 */
$output = apply_filters( \'gallery_style\', $gallery_style . $gallery_div );

$i = 0;
$first_image = reset($attachments);
$output .= \'<a href="" itemprop="image" class="woocommerce-main-image zoom">\';
$output .= wp_get_attachment_image( $first_image->ID, array(470,365), true, false );
$output .= \'</a>\';

$output .= \'<div class="thumbnails">\';

foreach ( $attachments as $id => $attachment ) {
    if ( ! empty( $link ) && \'file\' === $link )
        $image_output = wp_get_attachment_link( $id, $size, false, false );
    elseif ( ! empty( $link ) && \'none\' === $link )
        $image_output = wp_get_attachment_image( $id, $size, false );
    else
        $image_output = wp_get_attachment_link( $id, $size, true, false );

    $image_meta  = wp_get_attachment_metadata( $id );

    $orientation = \'\';
    if ( isset( $image_meta[\'height\'], $image_meta[\'width\'] ) )
        $orientation = ( $image_meta[\'height\'] > $image_meta[\'width\'] ) ? \'portrait\' : \'landscape\';

    $output .= $image_output;
}

if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
    $output .= "
        <br style=\'clear: both\' />";
}

$output .= "
    </div>\\n</div>\\n";

return $output;
}

1 个回复
最合适的回答,由SO网友:Todd Rowan 整理而成

您没有将属性数组传递给shortcode_atts 方法您正在传入一个未初始化的变量。

您的函数从

function get_my_gallery_content ( $atts ) {
但你不能参考$atts 再一次代码正在读取$attr, 它是空的。

更改线路

), $attr, \'gallery\'));

), $atts, \'gallery\'));
您还需要更改以下内容以匹配参数变量名:

if ( isset( $attr[\'orderby\'] ) ) {
    $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
    if ( !$attr[\'orderby\'] )
        unset( $attr[\'orderby\'] );
}
此代码读取$attr 但你要过去$atts. 确保他们都匹配。

因为数组是空的,所以逻辑只能返回到只检索当前帖子的子项。这就是为什么你只能在你的画廊里看到这些。

结束

相关推荐

watermarking gallery items

如何使用中的函数仅向要在库中发布的图像添加图像水印functions.php? 图像可以在.jpg, .png, .gif 总体安排我也found this snippet 它提供相同的服务,但对所有上传的图像进行水印处理。