如何在Functions.php中使用PHP手动修复WordPress库代码?

时间:2012-02-26 作者:its_me

Wordpress为内置的gallery功能输出了一些非常糟糕的代码,这已经被谈论了很多次了。

这是负责库输出的核心代码(在/wp-includes/media.php中):

function gallery_shortcode($attr) {
    global $post;

    static $instance = 0;
    $instance++;

    // Allow plugins/themes to override the default gallery template.
    $output = apply_filters(\'post_gallery\', \'\', $attr);
    if ( $output != \'\' )
        return $output;

    // 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\'] );
    }

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr));

    $id = intval($id);
    if ( \'RAND\' == $order )
        $orderby = \'none\';

    if ( !empty($include) ) {
        $include = preg_replace( \'/[^0-9,]+/\', \'\', $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) ) {
        $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

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

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', true ) )
        $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;
            }
        </style>
        <!-- see gallery_shortcode() in wp-includes/media.php -->";
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'wp-caption-text gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
    }

    $output .= "
            <br style=\'clear: both;\' />
        </div>\\n";

    return $output;
}
我想修复什么[1] 上面的代码将css样式直接输出到帖子中。我想阻止这种情况,因为我可以轻松地在我的样式中添加相同的css代码。css样式表。

[2] 我想禁止代码在缩略图下方输出图像标题。我希望标题只显示在附件页上,而不是在帖子中。

[3] 上述代码添加了两个<br style="clear: both;"> 库代码后的元素。我还想禁用它,因为我可以在css代码中使用“margin”。

我想在functions.php 文件,因为不建议编辑核心文件。

希望有人能帮忙。(我不知道如何编码,所以请尽可能清楚。)谢谢

RELEVANT: 要查看的源文件是/wp-includes/media.php (以下是trunk version -- 寻找gallery_shortcode 函数)。

4 个回复
最合适的回答,由SO网友:Bainternet 整理而成

就像在删除短代码并重新添加之前提到的那样,它与修改库的其他插件不兼容,因此您可以使用post_gallery 过滤器挂钩和来自gallery_shortcode 功能,但通过您自己的修改,例如,我已经注释掉了您不想要的部分:

function fix_my_gallery_wpse43558($output, $attr) {
    global $post;

    static $instance = 0;
    $instance++;


    /**
     *  will remove this since we don\'t want an endless loop going on here
     */
    // Allow plugins/themes to override the default gallery template.
    //$output = apply_filters(\'post_gallery\', \'\', $attr);

    // 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\'] );
    }

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr));

    $id = intval($id);
    if ( \'RAND\' == $order )
        $orderby = \'none\';

    if ( !empty($include) ) {
        $include = preg_replace( \'/[^0-9,]+/\', \'\', $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) ) {
        $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

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

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', true ) )
        /**
         * this is the css you want to remove
         *  #1 in question
         */
        /*
        $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;
            }
        </style>
        <!-- see gallery_shortcode() in wp-includes/media.php -->";
        */
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        /*
         * This is the caption part so i\'ll comment that out
         * #2 in question
         */
        /*
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'wp-caption-text gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }*/
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
    }

    /**
     * this is the extra br you want to remove so we change it to jus closing div tag
     * #3 in question
     */
    /*$output .= "
            <br style=\'clear: both;\' />
        </div>\\n";
     */

    $output .= "</div>\\n";
    return $output;
}
add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);

SO网友:Rob Vermeer

请参见badlearner\'s在下面编辑

您可以删除默认的短代码并创建自己的短代码。就像这样(在functions.php中):

remove_shortcode( \'gallery\' );
function my_own_gallary() {
    // Gallery code
}
add_shortcode( \'gallery\' , \'my_own_gallary\' );
更改短代码的最简单方法是将其复制粘贴到函数中。php并将函数名称更改为my_own_gallary 然后开始编辑。

正如goldenapples在评论中指出的那样:库短代码有一个过滤器,因此无需先删除短代码。

可以在函数中使用的示例。php(输出是默认的库短代码,所以您可以修改它)。

function my_own_gallery($output, $attr) {
    global $post;

    static $instance = 0;
    $instance++;

    // 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\'] );
    }

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr));

    $id = intval($id);
    if ( \'RAND\' == $order )
        $orderby = \'none\';

    if ( !empty($include) ) {
        $include = preg_replace( \'/[^0-9,]+/\', \'\', $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) ) {
        $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

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

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', true ) )
        $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;
            }
        </style>
        <!-- see gallery_shortcode() in wp-includes/media.php -->";
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class=\'wp-caption-text gallery-caption\'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'<br style="clear: both" />\';
    }

    $output .= "
            <br style=\'clear: both;\' />
        </div>\\n";

    return $output;
}
add_filter("post_gallery", "my_own_gallery",10,2);
解释:在WordPress定义的快捷码中,您将看到:

$output = apply_filters(\'post_gallery\', \'\', $attr);
    if ( $output != \'\' )
        return $output;
这意味着,如果应用了过滤器并返回了一些内容,那么将使用这些内容(返回),否则函数将继续(默认短代码)。

要添加过滤器,请使用add_filter 作用第一个参数是过滤器的标记(在本例中为“post\\u gallery”),第二个参数是要添加的函数(将返回自定义库输出的函数)。

因此,这将为短代码[库]输出“test”:

function my_own_gallery($output, $attr) {
    return \'test\';
}
add_filter("post_gallery", "my_own_gallery",10,2);
在我下面的示例“编辑”中,您将看到add\\u过滤器,它使用您自己的可编辑代码创建默认的短代码。你可以编辑这个,也可以从头开始。

<小时>(EDIT by Otto: 上述问题现已由奥托解决@RobVermeer缺少筛选器的第一个参数,并且没有正确执行add\\u筛选器。post\\u gallery过滤器是执行此操作的正确方法。删除短代码并重新添加(正如下面badlearner尝试的那样)是不可取的,因为它与修改库的其他插件也不兼容。)

添加/编辑人badlearner:使用函数修改库代码的过滤方法(在/wp-includes/media.php中)。@RobVermeer提供的php似乎工作不正常(请参阅此答案的注释)。

但是@RobVermeer的第一个答案(即,在第一次编辑之前)起到了作用,该答案取消了画廊短代码的注册并注册了一个新的画廊短代码。这是代码Please feel free to edit or add an answer if there\'s a better way.

以下是需要添加到主题函数中的代码。php文件:

<?php
remove_shortcode( \'gallery\' );
add_shortcode( \'gallery\' , \'my_own_gallary\' );
function my_own_gallary($attr) {
    global $post;

    static $instance = 0;
    $instance++;

    // Allow plugins/themes to override the default gallery template.
    $output = apply_filters(\'post_gallery\', \'\', $attr);
    if ( $output != \'\' )
        return $output;

    // 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\'] );
    }

    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
        \'include\'    => \'\',
        \'exclude\'    => \'\'
    ), $attr));

    $id = intval($id);
    if ( \'RAND\' == $order )
        $orderby = \'none\';

    if ( !empty($include) ) {
        $include = preg_replace( \'/[^0-9,]+/\', \'\', $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) ) {
        $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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);
    $columns = intval($columns);
    $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    $float = is_rtl() ? \'right\' : \'left\';

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

    $gallery_style = $gallery_div = \'\';
    if ( apply_filters( \'use_default_gallery_style\', true ) )
        $gallery_style = "
        <style type=\'text/css\'>
            #{$selector} .gallery-item {
                width: {$itemwidth}%;
            }
        </style>";
    $size_class = sanitize_html_class( $size );
    $gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}\'>";
    $output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );

    $i = 0;
    foreach ( $attachments as $id => $attachment ) {
        $link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

        $output .= "<{$itemtag} class=\'gallery-item\'>";
        $output .= "
            <{$icontag} class=\'gallery-icon\'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= \'\';
    }

    $output .= "
            <div style=\'clear:both;\'></div>
        </div>\\n";

    return $output;
}
?>

SO网友:fja3omega

在3.8版本的新wordpress中,它仍然存在,我所做的修复使用了上面相同的代码,但添加了几行

remove_shortcode( "gallery" );
add_shortcode( "gallery" , "my_own_gallary" );
function my_own_gallary( $attr ) {
global $post;
static $instance = 0;
$instance++;
$output = apply_filters(\'post_gallery\', \'\', $attr);
if ( $output != \'\' ) {
    return $output;
}
if ( isset( $attr[\'orderby\'] ) ) {
    $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
    if ( !$attr[\'orderby\'] )
        unset( $attr[\'orderby\'] );
}
extract(shortcode_atts(array(
    \'order\'      => \'ASC\',
    \'orderby\'    => \'menu_order ID\',
    \'id\'         => $post->ID,
    \'itemtag\'    => \'dl\',
    \'icontag\'    => \'dt\',
    \'captiontag\' => \'dd\',
    \'columns\'    => 3,
    \'size\'       => \'thumbnail\',
    \'include\'    => \'\',
    \'exclude\'    => \'\'
), $attr));
$id = intval($id);
if ( \'RAND\' == $order ) {
    $orderby = \'none\';
 }
if ( !empty($include) ) {
$include = preg_replace( \'/[^0-9,]+/\', \'\', $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) ) {
    $exclude = preg_replace( \'/[^0-9,]+/\', \'\', $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);
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? \'right\' : \'left\';
$selector = "gallery-".$instance;
$gallery_style = $gallery_div = \'\';
if ( apply_filters( \'use_default_gallery_style\', true ) )
    $gallery_style = "
    <style type=\'text/css\'>
        #".$selector." .gallery-item {
            width: ".$itemwidth."%;
        }
    </style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id=\'$selector\' class=\'gallery galleryid-".$id." gallery-columns-".$columns." gallery-size-".$size_class."\'>";
$output = apply_filters( \'gallery_style\', $gallery_style . "\\n\\t\\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr[\'link\']) && \'file\' == $attr[\'link\'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<".$itemtag." class=\'gallery-item\'>";
$output .= "
        <".$icontag." class=\'gallery-icon\'>
            $link
        </".$icontag.">";
/* added the <dd> here to fix validation error */
    if ( $captiontag && trim($attachment->post_excerpt) ) {
        $output .= "
            <".$captiontag." class=\'wp-caption-text gallery-caption\'>
            " . wptexturize($attachment->post_excerpt) . "
            </".$captiontag.">";
    } else {
        $output .= "
            <".$captiontag." class=\'wp-caption-text gallery-caption\' style=\'display:none;\'></".$captiontag.">";
    }
    $output .= "</".$itemtag.">";
    if ( $columns > 0 && ++$i % $columns == 0 )
        $output .= \'\';
}

$output .= "
        <div style=\'clear:both;\'></div>
    </div>\\n";

return $output;
}

SO网友:user3595293

add_filter( \'use_default_gallery_style\', \'__return_false\' );

结束

相关推荐

the_excerpt and shortcodes

我正在使用索引页上的\\u摘录。我还在我的每一篇文章的开头使用dropcap快捷码。在索引页面上,帖子不会显示周围带有dropcap快捷码的信件。如果我的帖子中有“Dog”一词,索引页会显示“og”。在使用\\u摘录时,如何使用短代码?短代码 function drcap ($atts, $content = null) { return \'<div class=\"dropcap\">\' . do_shortcode($content) . \'</div&g