Title: gallery機能で挿入されるaタグの内容を変更したい
Last modified: 2010年10月1日

---

# gallery機能で挿入されるaタグの内容を変更したい

 *  解決済 [hforum](https://ja.wordpress.org/support/users/hforum/)
 * (@hforum)
 * [16年、 1ヶ月前](https://ja.wordpress.org/support/topic/gallery%e6%a9%9f%e8%83%bd%e3%81%a7%e6%8c%bf%e5%85%a5%e3%81%95%e3%82%8c%e3%82%8ba%e3%82%bf%e3%82%b0%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%9f%e3%81%84/)
 * 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件中)

 *  [Fumito MIZUNO](https://ja.wordpress.org/support/users/php-web/)
 * (@php-web)
 * [16年、 1ヶ月前](https://ja.wordpress.org/support/topic/gallery%e6%a9%9f%e8%83%bd%e3%81%a7%e6%8c%bf%e5%85%a5%e3%81%95%e3%82%8c%e3%82%8ba%e3%82%bf%e3%82%b0%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%9f%e3%81%84/#post-132003)
 * wp_get_attachment_image_src を使えば、a href のつかない形で画像のURLが取得でき
   ます。
    戻り値が配列なので、その点だけ気をつけてください。
 * [http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src](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](https://ja.wordpress.org/support/users/hforum/)
 * (@hforum)
 * [16年、 1ヶ月前](https://ja.wordpress.org/support/topic/gallery%e6%a9%9f%e8%83%bd%e3%81%a7%e6%8c%bf%e5%85%a5%e3%81%95%e3%82%8c%e3%82%8ba%e3%82%bf%e3%82%b0%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%9f%e3%81%84/#post-132025)
 * 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タグの内容を変更したい」には新たに返信すること
はできません。

## タグ

 * [ギャラリー](https://ja.wordpress.org/support/topic-tag/%e3%82%ae%e3%83%a3%e3%83%a9%e3%83%aa%e3%83%bc/)
 * [functions.php](https://ja.wordpress.org/support/topic-tag/functions-php/)
 * [gallery](https://ja.wordpress.org/support/topic-tag/gallery/)
 * [shortcode](https://ja.wordpress.org/support/topic-tag/shortcode/)
 * [wp_get_attachment_link](https://ja.wordpress.org/support/topic-tag/wp_get_attachment_link/)

 * フォーラム: [使い方全般](https://ja.wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2件の返信
 * 2人の参加者
 * 最後の返信者: [hforum](https://ja.wordpress.org/support/users/hforum/)
 * 最後の活動: [16年、 1ヶ月前](https://ja.wordpress.org/support/topic/gallery%e6%a9%9f%e8%83%bd%e3%81%a7%e6%8c%bf%e5%85%a5%e3%81%95%e3%82%8c%e3%82%8ba%e3%82%bf%e3%82%b0%e3%81%ae%e5%86%85%e5%ae%b9%e3%82%92%e5%a4%89%e6%9b%b4%e3%81%97%e3%81%9f%e3%81%84/#post-132025)
 * 状態: 解決済

## トピック

### 人気のトピック

### 返信がないトピック

### サポート関連ではないトピック

### 解決済みのトピック

### 未解決のトピック

### すべてのトピック
