Remove Captions from Custom Gallery function.php

admin2025-01-08  5

I like to prevent captions from being outputted in my custom gallery function. I could easily just hide it using CSS but it doesn't feel right.

The caption is being sent from media.php this line.

        if ( $captiontag && trim($attachment->post_excerpt) ) {
        $output .= "
            <{$captiontag} class='wp-caption-text gallery-caption'>
            " . wptexturize($attachment->post_excerpt) . "
            </{$captiontag}>";
    }

I could just remove . wptexturize($attachment->post_excerpt) . but as soon as I update its gone. Id like to to this without copying the whole gallery function.

Wordpress is generating the alt tag using the caption field, which is desired.

Is there a way?

My function code

function modified_gallery_shortcode( $attr ) {

$attr['size']   = "thumbnail";
$attr['link']   = "file";
$attr['itemtag']  = "";
$attr['icontag']  = "";
$attr['captiontag'] = "";
$output = gallery_shortcode( $attr );
$output = strip_tags( $output, '<a><img><li><p>' );
$from = array(  
        "class='gallery-item'", 
        "class='gallery-icon landscape'", 
        "a href=",
        "class='wp-caption-text gallery-caption'"
    );      
$to = array( 
      "",
      "",
      "a class=\"swipebox\" rel=\"group\" href=",
      "",
);
$output = str_replace( $from, $to, $output );
$output = sprintf( '<div class="gallery">%s</div>', $output );
return $output;
}

add_shortcode( 'gallery', 'modified_gallery_shortcode' );

I like to prevent captions from being outputted in my custom gallery function. I could easily just hide it using CSS but it doesn't feel right.

The caption is being sent from media.php this line.

        if ( $captiontag && trim($attachment->post_excerpt) ) {
        $output .= "
            <{$captiontag} class='wp-caption-text gallery-caption'>
            " . wptexturize($attachment->post_excerpt) . "
            </{$captiontag}>";
    }

I could just remove . wptexturize($attachment->post_excerpt) . but as soon as I update its gone. Id like to to this without copying the whole gallery function.

Wordpress is generating the alt tag using the caption field, which is desired.

Is there a way?

My function code

function modified_gallery_shortcode( $attr ) {

$attr['size']   = "thumbnail";
$attr['link']   = "file";
$attr['itemtag']  = "";
$attr['icontag']  = "";
$attr['captiontag'] = "";
$output = gallery_shortcode( $attr );
$output = strip_tags( $output, '<a><img><li><p>' );
$from = array(  
        "class='gallery-item'", 
        "class='gallery-icon landscape'", 
        "a href=",
        "class='wp-caption-text gallery-caption'"
    );      
$to = array( 
      "",
      "",
      "a class=\"swipebox\" rel=\"group\" href=",
      "",
);
$output = str_replace( $from, $to, $output );
$output = sprintf( '<div class="gallery">%s</div>', $output );
return $output;
}

add_shortcode( 'gallery', 'modified_gallery_shortcode' );
Share Improve this question edited Aug 3, 2014 at 17:29 rgfx asked Aug 3, 2014 at 5:37 rgfxrgfx 1031 silver badge8 bronze badges 7
  • what is the full code of your custom gallery function? – Michael Commented Aug 3, 2014 at 15:09
  • Why can't you just set captiontag=""? – user27457 Commented Aug 3, 2014 at 17:08
  • @MagentaCuda I tried but it still spits out the the caption text in question marks " caption text " – rgfx Commented Aug 3, 2014 at 17:27
  • 1 Also, could some other plugin be doing an additional add_shortcode( 'gallery', ... ) after yours and overriding yours. I really don't understand why setting captiontag = "" doesn't work unless I am reading the wrong source code. By the way what is your WordPress version number. I am looking at 3.9 source code. – user27457 Commented Aug 3, 2014 at 22:53
  • 1 @MagentaCuda Your right, I was using a template called bones that added its own filter to strip out gallery styles. I removed it and add this add_filter( 'use_default_gallery_style', '__return_false' ); and my problem is fixed. Thanks for your help, I prob would of never figured it out if it wasn't for you. – rgfx Commented Aug 4, 2014 at 5:05
 |  Show 2 more comments

1 Answer 1

Reset to default 0

if you can do it easy way ,so why not?

you can use jquery to do it like:

( function( $ ) {    
    function function_name() {
          $( '.gallery-caption').css({display:'none'});
}

function_name();
} )( jQuery );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270760a1453.html

最新回复(0)