WordPress.org

Ready to get started?Download WordPress

Forums

Display Title as Caption in WP Gallery (9 posts)

  1. mstg2000
    Member
    Posted 3 years ago #

    Is there a way.. coding or plugin.. to display the image "Title" as the caption while using the standard WP Gallery?

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Not without completely rewriting the gallery shortcode as part of your theme.

  3. mstg2000
    Member
    Posted 3 years ago #

    hmmmm.. when you upload an image, the title automatically is set based on the filename. wish there was a way for the caption to be auto set as well.

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    You can set the caption to the title using the following.

    add_filter('attachment_fields_to_edit', 'image_caption_to_title', 11, 2 );
    
    function image_caption_to_title( $form_fields, $post ) {
    	if ( substr( $post->post_mime_type, 0, 5 ) == 'image' ) {
    		$form_fields['post_excerpt']['value'] = $post->post_title;
    		//$form_fields['post_excerpt']['input'] = 'hidden';
    	}
    	return $form_fields;
    }

    You can also hide the caption field by changing //$form_fields['post_excerpt']['input'] = 'hidden'; for $form_fields['post_excerpt']['input'] = 'hidden';.

    All it will do is set the default value of the caption to that of the title(it doesn't stop a user changing it, but you can hide the field to prevent it as i mentioned above).

    Hope that helps..

  5. mstg2000
    Member
    Posted 3 years ago #

    Mark, thank you very much'
    i hate to sound like a newb, but which file do i add this too? functions.php?

  6. mstg2000
    Member
    Posted 3 years ago #

    nevermind, it worked great, thank you!
    now if only there was a way to get it to work retroactively to my existingn uploads, hehe

  7. Mark, you just saved what's left of my sanity - this is exactly what I've been searching for. Thanks :-)

  8. rebbekka
    Member
    Posted 3 years ago #

    I am a newbie to WP ~ what is the answer to where the code gets added?

    Is there a plug-in that can modify existing images in the media library?

    Thanks in advance.

  9. mstg2000
    Member
    Posted 3 years ago #

    Place that code into your functions.php file, save it.
    Upload an image, after it's done crunching Save it again.. your title and caption will end up as whatever your title was.

    I've gotten into the habit of simply naming my images the same as I want it captioned, works great.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.