WordPress.org

Forums

Add Caption (Title) to Post Thumbnails (4 posts)

  1. robparker
    Member
    Posted 4 years ago #

    Hi, I'm trying to achieve the result where I've got the post thumbnail outputted, linked to the large size file and then below it, outputted the title given to that image.

    I've got the code from Codex for the first part (link to large image), but whatever I try I can't get the image title to display next. It already outputs it as the image title=".." info but I can't get it as text on its own.

    Any help very much appreciated!

    Thanks

  2. wlanni
    Member
    Posted 3 years ago #

    +1 to this.

    I've attempted to add a custom function to my functions.php that adds the title into the img_caption_shortcode, but it just returns a blank, no title:

    function fb_img_caption_shortcode($attr, $content = null) {
        // Allow plugins/themes to override the default caption template.
        $output = apply_filters('img_caption_shortcode', '', $attr, $content);
        if ( $output != '' )
        return $output;
        extract(shortcode_atts(array(
        'id' => '',
        'align' => 'alignnone',
        'width' => '',
        'caption' => '',
        'title' => ''
        ), $attr));
        if ( 1 > (int) $width || empty($caption) )
        return $content;
        if ( $id ) $id = 'id="' . $id . '" ';
        return '<dl ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px"><dt>' . do_shortcode( $content ) . '</dt><dd class="wp-caption-text"><em>' . $title . '</em>' . $caption . '</dd></dl>';
    }
    add_shortcode('wp_caption', 'fb_img_caption_shortcode');
    add_shortcode('caption', 'fb_img_caption_shortcode');

    Does anyone know how to grab the title out of the media library's title field, and add it to the caption?

  3. alchymyth
    Forum Moderator
    Posted 3 years ago #

  4. wlanni
    Member
    Posted 3 years ago #

    Hey alchymyth, thanks for that.

    I saw that during my search over the last day. Unfortunately, this doesn't solve my issue, as the solution posted at the bottom of that page by MosDave is more suitable to be placed in a template page.

    I'm trying to filter and modify the caption via my functions.php file, throughout the entire site. I am adding thumbnails via the page editor on multiple pages, not running a query via the loop.

    I'm curious how to use the solution that MosDave posted in combination with the filter to the wp_caption code... any clue?

Topic Closed

This topic has been closed to new replies.

About this Topic