WordPress.org

Forums

[resolved] Native gallery shortcode output- title attribute (5 posts)

  1. chachalady
    Member
    Posted 1 year ago #

    Hello,
    I would like to avoid installation of any other gallery plugin since Native wordpress gallery does everything I need (I even managed to add fancybox class and rel ...) EXCEPT ONE THING :
    - there is no title attribute (and I need one in a> tag ... )

    I know it is a "wordpress way" now : gallery shortcode generate the output without title , text we write inside image title is outputed in 'alt' like this:

    <a class="fancybox" href="http://www.mydomain.com/..my-image.jpg" rel="galleryGroup">
    <img class="attachment-thumbnail" width="150" height="150" alt="MY TITLE" src="http://www.mydomain.com/..my-image-thumb.jpg">
    </a>

    I need this:

    <a class="fancybox" title="MY TITLE" href="http://www.mydomain.com/..my-image.jpg" rel="galleryGroup">
    <img class="attachment-thumbnail" width="150" height="150" alt="MY TITLE" src="http://www.mydomain.com/..my-image-thumb.jpg">
    </a>

    Since the title is "picked up" in alt there must be the way to use it and write/ echo it in title attribute.

    I really need it to work with gallery shortcode, when inserting gallery to post or page.
    ( title attr. while inserting single image is not so important right now ... it can be added manualy if needed ...)

    If it means to hack the core wordpress file - so be it.!
    I can live with that one and only hack - I will add it when updating wordpress ...

    If there is other way please help - I was browsing through forums for days now with no luck ( either I messed up with code or syntax or the solution was not what I need ..)

    I believe the code for gallery shortcode output is in wp-includes/media.php - don't know how to get the title from the image data and display it as title attribute in tag ...

    Thanks,
    ChachaLady

  2. chachalady
    Member
    Posted 1 year ago #

    Few minutes after I posted a question I found the solution !!!!!! :-)

    (last solution inserted title to <img> tag, not anchor, but this one is doing the exact thing I need ... )

    Here is the code - I put it in functions.php and is working great, now my fancybox shows title of the images inserted in media:

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

    Hope this will help someone, I found the solution here.

    Happy wordpressing :-)

  3. Pat K
    Member
    Posted 1 year ago #

    Hi chachalady! You officially helped someone! Thanks for sharing this. Works like a charm.

    :)

  4. benbunch
    Member
    Posted 1 year ago #

    Thanks man, this was the key to my riddle.

    Not a lot of documentation on this strategy of implementing Lightbox with the attachment link attribute filter. I prefer to use as few plugins as possible, so this is the way to go.

    Much appreciated!

  5. chachalady
    Member
    Posted 1 year ago #

    Dear @BlackCapDesign and @benbunch,
    thank you for your input - it is so good to know that I helped someone too.
    I am more-less beginer myself and if you give me the code I can understand what it does and where to use it, but I still have much to learn to be able to write some complex functions. Here on wordpress.org forums I found solutions to many problems and I was so happy to find good people willing to help me - this is my small contribution :-)

    Happy wordpressing :-)

Topic Closed

This topic has been closed to new replies.

About this Topic