Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Wordpress default gallery output

I am looking to use the Wordpress gallery shortcut but I want to tie the output into the Foundation Orbit plugin (to make a slider). This is the HTML I am looking to output:

<div class="slideshow-wrapper">     <div class="preloader"></div>     <ul data-orbit>         <li>             <img src="img1.png" alt="bla bla bla" />         </li>         <li>             <img src="img2.png" alt="bla bla bla" />         </li>         <li>             <img src="img3.png" alt="bla bla bla" />         </li>         <li>             <img src="img4.png" alt="bla bla bla" />         </li>     </ul> </div> 

Is it possible to put something in functions.php (or similar) to achieve this?

like image 256
Sheixt Avatar asked Nov 06 '13 01:11

Sheixt


People also ask

How do I change my WordPress gallery?

When you click any area of the image gallery, icon buttons appear top of the area. At any time, you can edit the images or settings of your gallery by clicking on the Edit button. You can remove the image gallery at any time by clicking on the Remove button.

How do I change the columns in a gallery in WordPress?

If you want to change the number of columns or other settings, then you just need to click on the photos in post editor. WordPress will select your gallery and you can then click on the pencil icon to edit your gallery settings. That's all, you can continue writing your post or save your changes.

How do I access my WordPress gallery?

First, you need to edit the post or page where you want to add the image gallery. On the post edit screen, click on the Add New Block icon and select the Gallery block. This will add the Gallery block to your WordPress editor where you can click on the 'Upload' button to upload photos from your computer.

What is Post Gallery in WordPress?

(10 ) Adds Gallery of pictures, photos and videos to any post type.


1 Answers

Yes, indeed. Quite a while ago I've found this code and have been using it ever since. It's great to customize WP's default gallery to whatever you want.

There's a filter to post_gallery which you can use to customize all default WP galleries. Here's a sample of the code I use adapted to the template you provided. I've cleared it up as much as possible.

The first part of the function is pretty much gallery attachments handling, so you'll probably just want to change the latter half, the one that determines the output of your gallery template (follow the comments):

add_filter('post_gallery', 'my_post_gallery', 10, 2); function my_post_gallery($output, $attr) {     global $post;      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];         }     }      if (empty($attachments)) return '';      // Here's your actual output, you may customize it to your need     $output = "<div class=\"slideshow-wrapper\">\n";     $output .= "<div class=\"preloader\"></div>\n";     $output .= "<ul data-orbit>\n";      // Now you loop through each attachment     foreach ($attachments as $id => $attachment) {         // Fetch the thumbnail (or full image, it's up to you) //      $img = wp_get_attachment_image_src($id, 'medium'); //      $img = wp_get_attachment_image_src($id, 'my-custom-image-size');         $img = wp_get_attachment_image_src($id, 'full');          $output .= "<li>\n";         $output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n";         $output .= "</li>\n";     }      $output .= "</ul>\n";     $output .= "</div>\n";      return $output; } 

Just paste it to your functions.php file and modify to adapt it to your need. I'm pretty sure it'll work for you as it have worked for me :)

like image 51
mathielo Avatar answered Oct 02 '22 12:10

mathielo