サポート » 使い方全般 » gallery機能で挿入されるaタグの内容を変更したい

  • 解決済 hforum

    (@hforum)


    gallery機能をカスタマイズして使用したいのですが、なかなか上手くいきません。
    themesの中のfunctions.phpにgalleryのショートコードを記述しています。

    実現したいことは、生成されるaタグの内容を変更することです。
    今現在は、

    <div class="recent_pic smallphoto">
        <a href="http://example.jp/wp-content/uploads/2010/09/19.jpg" title="19">
            <img src="http://example.jp/wp-content/uploads/2010/09/19-100x150.jpg" class="attachment-medium" alt="19" title="19" width="100" height="150">
        </a>
        <br style="clear: both;">
    </div>

    というhtmlが出力されますが、これを

    <div class="recent_pic smallphoto">
        <a href="#main" onclick="clickImage('largephoto','','http://example.jp/wp-content/uploads/2010/09/test.jpg',1)">
            <img src="http://example.jp/wp-content/uploads/2010/09/test-100x150.jpg" class="attachment-medium" alt="19" title="19" width="100" height="150">
        </a>
        <br style="clear: both;">
    </div>

    というhtmlが出力されるように変更したいのです。

    functions.phpには

    add_shortcode('mygallery', 'my_gallery_shortcode');
    function my_gallery_shortcode($attr) {
        global $post, $wp_locale;
    
        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,
            'columns'    => 3,
            'size'       => 'medium',
            '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}";
    
        $output = apply_filters('gallery_style', "
            <div class='recent_pic smallphoto'>");
    
        $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 .= $link;
    
            if ( $columns > 0 && ++$i % $columns == 0 )
                $output .= '<br style="clear: both" />';
        }
    
        $output .= "</div>\n";
    
        return $output;
    }
    add_shortcode( 'mygallery', 'my_gallery_shortcode' );

    themes内のsingle.phpには
    <?php echo do_shortcode('[mygallery link="file"]'); ?>
    と記述しています。

    wp_get_attachment_linkを色々と変更して試したのですが、思うようにいきませんでした。
    どうか、皆様のお知恵をお貸しください。
    宜しくお願い致します。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • wp_get_attachment_image_src を使えば、a href のつかない形で画像のURLが取得できます。
    戻り値が配列なので、その点だけ気をつけてください。

    http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

    Return Values

    (array)
    An array containing:

    * $image[0] => url
    * $image[1] => width
    * $image[2] => height

    トピック投稿者 hforum

    (@hforum)

    php-web様
    ありがとうございます。
    wp_get_attachment_image_srcを使用して、無事にURLだけ取得することができ、
    思い通りの挙動になりました。感謝いたします。
    以下、functions.phpに追記したコードを記述いたします。
    どなたかの参考になれば幸いです。

    foreach ( $attachments as $id => $attachment ) {
    
        $url_full = wp_get_attachment_image_src($id, $size='full');
        $url_medium = wp_get_attachment_image_src($id, $size='medium');
    
        $output .= "<a onclick=\"clickImage('largephoto','','";
        $output .= $url_full[0];
        $output .= "',1)\">";
        $output .= "<img src=\"";
        $output .= $url_medium[0];
        $output .= "\" width=\"100\" height=\"150\" />";
        $output .= "</a>";
        }

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「gallery機能で挿入されるaタグの内容を変更したい」には新たに返信することはできません。