如何更改插入到帖子中的图库?

时间:2012-07-10 作者:ilyo

当我使用[gallery] 短代码或简单的insert media 按钮我在博客中通过the_content();然而,图库中有很多内联CSS和其他我想删除的东西,我该怎么做呢?

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

您可以过滤默认的库短代码。这是我过去用过的东西。

add_filter( \'post_gallery\', \'wpse_gallery\', 10, 2 );

function wpse_gallery() {
    global $post;

    /* Orderby */
    if ( isset( $attr[\'orderby\'] ) ) :
        $attr[\'orderby\'] = sanitize_sql_orderby( $attr[\'orderby\'] );
        if ( !$attr[\'orderby\'] )
            unset( $attr[\'orderby\'] );
    endif;

    /*
    * Extract default gallery settings
    */
    extract(shortcode_atts(array(
        \'order\'      => \'ASC\',
        \'orderby\'    => \'menu_order ID\',
        \'id\'         => $post->ID,
        \'itemtag\'    => \'dl\',
        \'icontag\'    => \'dt\',
        \'captiontag\' => \'dd\',
        \'columns\'    => 3,
        \'size\'       => \'thumbnail\',
    ), $attr));

    /*
    * Make sure $id is an integer
    */
    $id = intval( $id );

    /*
    * Get image attachments
    * If none, return
    */
    $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, leave the default WP settings
    * We\'re only worried about on-site presentation
    */
    if ( is_feed() ) {
        $output = "\\n";
        foreach ( $attachments as $id => $attachment )
            $output .= wp_get_attachment_link( $id, $size, true ) . "\\n";
        return $output;
    }

    $i = 0;

    /*
    * Remove the style output in the middle of the freakin\' page.
    * This needs to be added to the header.
    * The width applied through CSS but limits it a bit.
    */

    /*
    * Open the gallery <div>
    */
    $output .= \'<div id="gallery-\'.$id.\'" class="content gallery gallery-\'.$id.\'">\'."\\n";
    $output .= \'<div id="thumbs" class="navigation">\'."\\n";
    $output .= \'<ul class="thumbs noscript">\'."\\n";
    /*
    * Loop through each attachment
    */
    foreach ( $attachments as $id => $attachment ) :

        /*
        * Get the caption and title
        */
        $caption = esc_html( $attachment->post_excerpt, 1 );
        $title = esc_html( $attachment->post_title, 1 );
        $link = wp_get_attachment_image_src( $id, \'large\' );
        $img = wp_get_attachment_image_src( $id, $size );

        /*
        * Open each gallery item
        */
        $output .= "\\n\\t\\t\\t\\t\\t<li class=\'gallery-item\'>";
            $output .= \'<a class="thumb" href="\' .  wp_get_attachment_url( $id ) . \'" title="\' . $title . \'">\';
                $output .= \'<img src="\' . $img[0] . \'" alt="\' . $title . \'" title="\' . $title . \'" />\';
            $output .= \'</a>\';

        /*
        * If image caption is set
        */
        if ( $caption ) :
            $output .= "\\n\\t\\t\\t\\t\\t\\t<div class=\'caption\'>";
                $output .= $caption;
            $output .= "</div>";
        endif;

        /*
        * Close individual gallery item
        */
        $output .= "\\n\\t\\t\\t\\t\\t</li>";

    endforeach;

    /*
    * Close gallery and return it
    */

        $output .= \'</ul><!--.thumbs-->\'."\\n";
        $output .= \'</div><!--#thumbs-->\'."\\n";
    /*
    * Return out very nice, valid XHTML gallery.
    */
    return $output;

}

结束

相关推荐

Change posts URL

Possible Duplicate:Permalink: postname EXCEPT for blog 我已经同意将我的网站转换为wordpress,并使用了索引。php作为主页和博客。php(博客帖子的模板),但当我创建任何新帖子时,它会生成类似permalink的页面,例如。。。http://www.domain.com/post-name 而不是http://www.domain.com/blog/post-1你能帮助我如何为所有博客帖子分配/写博客吗。谢谢你的帮助。