WordPress.org

Support

Support » How-To and Troubleshooting » Add separate rel to multiple galleries in one post

Add separate rel to multiple galleries in one post

  • I have a WordPress post/page that I want to have multiple galleries in. I am using the Multiple Galleries plugin to manage them (it basically includes the selected image id in each gallery shortcode instance). This produces the following html with different IDs for the separate galleries:

    <div id="gallery-1">
       <dl class="gallery-item">
        <dt class="gallery-icon">
         <a href="big-image.jpg"><img src="small-img.jpg" /></a>
        </dt>
       </dl>
       <dl> ...another image... </dl>
    </div>
    <div id="gallery-2">
       <dl class="gallery-item">
        <dt class="gallery-icon">
         <a href="big-image.jpg"><img src="small-img.jpg" /></a>
        </dt>
       </dl>
       <dl> ...another image... </dl>
    </div>
    ...and so on

    In addition I have a filter that adds a rel attribute to each anchor that links to an image:

    function slicetheme_attachment_filter($attachment_link)
    {
        global $post;
        $pattern = "/<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
        $replacement = '<a$1rel="fancybox[%LIGHTID%]" href=$2$3.$4$5  $6>$7</a>';
        $attachment_link= preg_replace($pattern, $replacement, $attachment_link);
        $attachment_link = str_replace("%LIGHTID%", $post->ID, $attachment_link);
        return $attachment_link;
    }
    add_filter('wp_get_attachment_link', 'slicetheme_attachment_filter');

    I modified it so I get a gallery from each post/page by adding a rel with the post ID. So for post with ID=553 I have:

    <a rel="fancybox[553]" href="big-image.jpg"><img src="small-img.jpg" /></a>
    But this is not what I want. I want each gallery to have a different rel attribute to its anchors. Something like:

    <div id="gallery-1">
         <dl class="gallery-item">
          <dt class="gallery-icon">
           <a rel="fancybox-gallery-1" href="big-image.jpg"><img src="small-img.jpg" /></a>
          </dt>
         </dl>
         <dl> ...another image... </dl>
     </div>
     <div id="gallery-2">
         <dl class="gallery-item">
          <dt class="gallery-icon">
           <a rel="fancybox-gallery-2" href="big-image.jpg"><img src="small-img.jpg" /></a>
          </dt>
         </dl>
         <dl> ...another image... </dl>
     </div>

    I tried to match the whole thing but I guess I’m not good at it…

    $pattern = "/<div(.*?)id=('|\")gallery-(.*?)('|\")(.*?)<a(.*?)href=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
    $replacement = '<div$1id=$2gallery-$3$4$5<a$6rel="fancybox-gallery-$3" href=$7$8.$9$10  $11>$12</a>';

    Please help. Thank you!

  • The topic ‘Add separate rel to multiple galleries in one post’ is closed to new replies.
Skip to toolbar