Posted By

lancemonotone on 01/25/10


Tagged

wordpress gallery shortcode


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

marclarr


Wordpress 2.7+ Gallery Styles Fix


 / Published in: PHP
 

URL: http://368design.com

This is a combination of two fixes from Michael at WPEngineer.com, which removes the styles from the gallery output, and a follow-up comment by Aaron Cimolini, which allows the developer to omit shortcode attributes.

The blog stripped some of the usable code from the comment, so I cleaned it up and reposted it here. I'm using it with the Sandbox theme, so I've replaced the original sandbox_gallery function with Michael and Aaron's improvement.

  1. // Registers our function to filter default gallery shortcode
  2. remove_shortcode('gallery');
  3. add_shortcode('gallery', 'sandbox_gallery');
  4.  
  5. // Function to filter the default gallery shortcode
  6. function sandbox_gallery($attr) {
  7. global $post;
  8.  
  9. static $instance = 0;
  10. $instance++;
  11.  
  12. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  13. if ( isset( $attr['orderby'] ) ) {
  14. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  15. if ( !$attr['orderby'] )
  16. unset( $attr['orderby'] );
  17. }
  18.  
  19. extract(shortcode_atts(array(
  20. 'order' => 'ASC',
  21. 'orderby' => 'menu_order ID',
  22. 'id' => $post->ID,
  23. 'itemtag' => 'dl',
  24. 'icontag' => 'dt',
  25. 'captiontag' => 'dd',
  26. 'columns' => 3,
  27. 'size' => 'thumbnail',
  28. 'include' => '',
  29. 'exclude' => ''
  30. ), $attr));
  31.  
  32. $id = intval($id);
  33. if ( 'RAND' == $order )
  34. $orderby = 'none';
  35.  
  36. if ( !empty($include) ) {
  37. $include = preg_replace( '/[^0-9,]+/', '', $include );
  38. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  39.  
  40. $attachments = array();
  41. foreach ( $_attachments as $key => $val ) {
  42. $attachments[$val->ID] = $_attachments[$key];
  43. }
  44. } elseif ( !empty($exclude) ) {
  45. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  46. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  47. } else {
  48. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  49. }
  50.  
  51. if ( empty($attachments) )
  52. return '';
  53.  
  54. if ( is_feed() ) {
  55. $output = "\n";
  56. foreach ( $attachments as $att_id => $attachment )
  57. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  58. return $output;
  59. }
  60.  
  61. // check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
  62. if ($itemtag) $itemtag = tag_escape($itemtag);
  63. if ($captiontag) $captiontag = tag_escape($captiontag);
  64. if ($icontag) $icontag = tag_escape($icontag);
  65. $columns = intval($columns);
  66.  
  67. $selector = "gallery-{$instance}";
  68.  
  69. $output = "<div id='$selector' class='gallery galleryid-{$id}'>\n";
  70.  
  71. $i = 0;
  72. foreach ( $attachments as $id => $attachment ) {
  73. ++$i;
  74. $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  75.  
  76. if ($itemtag) {
  77. $output .= '<'.$itemtag.' class="gallery-item ';
  78. if( $columns > 0 && $i % $columns == 0 ) $output .= " last";
  79. $output .= '">';
  80. }
  81. if ($icontag) $output .= "\n\t<" .$icontag. ">\t";
  82. $output .= "\n\t".$link;
  83. if ($icontag) $output .= "\n\t</".$icontag.">";
  84. // if the attachment has a caption set
  85. if ( trim($attachment->post_excerpt) ) {
  86. if ($captiontag) $output .= "\n<" .$captiontag. ">\n\t";
  87. $output .= wptexturize($attachment->post_excerpt);
  88. if ($captiontag) $output .= "\n</" .$captiontag. ">" . "<!-- end caption -->\n";
  89. }
  90. if ($itemtag) $output .= "\n</".$itemtag ."><!-- end itemtag -->\n";
  91. if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
  92. }
  93.  
  94. $output .= "</div><!-- end gallery -->\n";
  95.  
  96. return $output;
  97. }

Report this snippet  

You need to login to post a comment.