2

We have a client that is using a wysiwyg editor to add multiple photos and different bits to a custom post type.

They are including a caption using the Wordpress Image caption functionality.

How would we be able to (efficiently) use Fancybox's title option to do this?

Here is the structure:

<div id="attachment_180" class="wp-caption">
    <a href="image.jpg" rel="performance">
        <img class="size-full" src="someimagethumb.jpg">
    </a>
    <p class="wp-caption-text">The Image Caption</p>
</div>
Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
Robert C Edwards
  • 708
  • 2
  • 8
  • 17
  • that would be very simple using fancybox v2.x (you would need to hard-code it into WordPress). here it is how http://stackoverflow.com/a/9611664/1055987 – JFK Mar 29 '12 at 19:03

3 Answers3

2

I think you might want something like this

$('div.wp-caption').each(function(){
    var caption = $(this).find('p.wp-caption-text').text();
    $(this).find('img').attr('title',caption);
    $(this).find('p.wp-caption-text').remove();    
});​

Explanation:

For each pic div (as there may be many on the page), do the following

  1. capture the text from the caption as inserted
  2. add the text to title attribute in the img element
  3. remove the caption p element

Example: http://jsfiddle.net/ZVu9e/

Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
1

I ended up using a hook in the functions.php file instead of JQuery:

function add_title_attachment_link($link, $id = null) {
    $id = intval( $id );
    $_post = get_post( $id );
    $post_title = esc_attr( $_post->post_excerpt );
    return str_replace('<a href', '<a title="'. $post_title .'" href', $link);
}
add_filter('wp_get_attachment_link', 'add_title_attachment_link', 10, 2);

This will return the caption from the attachment and add it to the <a> tag as a title for most JQuery lightbox plugins.

For example, in Fancybox:

$(".fancybox").fancybox({
    helpers : {
        title : {
            type : 'over'
        }
    }
});

The caption will now appear in the lightbox.

RCNeil
  • 8,581
  • 12
  • 43
  • 61
0

Grab it, then pass to fancybox.

var txt = $( '.wp-caption-text' ).text();
//do fancybox with txt passed to the title property
Matthew Blancarte
  • 8,251
  • 2
  • 25
  • 34