Wordpress Gallery - Changing the CSS


/ Published in: PHP
Save to your folder(s)

Original code from: http://wpengineer.com/1802/a-solution-for-the-wordpress-gallery/


Copy this code and paste it in your HTML
  1. //deactivate WordPress function
  2. remove_shortcode('gallery', 'gallery_shortcode');
  3. //activate own function
  4. add_shortcode('gallery', 'wpe_gallery_shortcode');
  5. //the own renamed function
  6. function wpe_gallery_shortcode($attr) {
  7. global $post, $wp_locale;
  8.  
  9. static $instance = 0;
  10. $instance++;
  11.  
  12. // Allow plugins/themes to override the default gallery template.
  13. $output = apply_filters('post_gallery', '', $attr);
  14. if ( $output != '' )
  15. return $output;
  16.  
  17. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  18. if ( isset( $attr['orderby'] ) ) {
  19. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  20. if ( !$attr['orderby'] )
  21. unset( $attr['orderby'] );
  22. }
  23.  
  24. extract(shortcode_atts(array(
  25. 'order' => 'ASC',
  26. 'orderby' => 'menu_order ID',
  27. 'id' => $post->ID,
  28. 'itemtag' => 'dl',
  29. 'icontag' => 'dt',
  30. 'captiontag' => 'dd',
  31. 'columns' => 3,
  32. 'size' => 'thumbnail',
  33. 'include' => '',
  34. 'exclude' => ''
  35. ), $attr));
  36.  
  37. $id = intval($id);
  38. if ( 'RAND' == $order )
  39. $orderby = 'none';
  40.  
  41. if ( !empty($include) ) {
  42. $include = preg_replace( '/[^0-9,]+/', '', $include );
  43. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  44.  
  45. $attachments = array();
  46. foreach ( $_attachments as $key => $val ) {
  47. $attachments[$val->ID] = $_attachments[$key];
  48. }
  49. } elseif ( !empty($exclude) ) {
  50. $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  51. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  52. } else {
  53. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  54. }
  55.  
  56. if ( empty($attachments) )
  57. return '';
  58.  
  59. if ( is_feed() ) {
  60. $output = "\n";
  61. foreach ( $attachments as $att_id => $attachment )
  62. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  63. return $output;
  64. }
  65.  
  66. $itemtag = tag_escape($itemtag);
  67. $captiontag = tag_escape($captiontag);
  68. $columns = intval($columns);
  69. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  70. $float = is_rtl() ? 'right' : 'left';
  71.  
  72. $selector = "gallery-{$instance}";
  73.  
  74. $gallery_style = $gallery_div = '';
  75. if ( apply_filters( 'use_default_gallery_style', true ) )
  76. $gallery_style = "
  77. <style type='text/css'>
  78. </style>
  79. <!-- see wpe_gallery_shortcode() in wp-includes/media.php -->";
  80. $size_class = sanitize_html_class( $size );
  81. $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  82. $output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
  83.  
  84. $i = 0;
  85. foreach ( $attachments as $id => $attachment ) {
  86. $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
  87.  
  88. $output .= "<{$itemtag} class='gallery-item'>";
  89. $output .= "
  90. <{$icontag} class='gallery-icon'>
  91. $link
  92. </{$icontag}>";
  93. if ( $captiontag && trim($attachment->post_excerpt) ) {
  94. $output .= "
  95. <{$captiontag} class='wp-caption-text gallery-caption'>
  96. " . wptexturize($attachment->post_excerpt) . "
  97. </{$captiontag}>";
  98. }
  99. $output .= "</{$itemtag}>";
  100. if ( $columns > 0 && ++$i % $columns == 0 )
  101. $output .= '';
  102. }
  103.  
  104. $output .= "
  105.  
  106. </div>\n";
  107.  
  108. return $output;
  109. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.