Support » Plugin: Related Posts by Taxonomy » Add data attribute to item link

  • studioavanti

    (@studioavanti)


    Hi,

    Thanks for this useful and efficient plugin, nice work!

    I’m trying to add a Bootstrap Tooltip to the items of a thumbnails gallery.
    So i need to add this data attribute inside the <a> tag: data-toggle="tooltip"
    I don’t really know where to begin, i tried to customize the template but maybe a simple filter would do the job?

    Any advice would be welcome.

Viewing 5 replies - 1 through 5 (of 5 total)
  • studioavanti

    (@studioavanti)

    I get this filter working to add the data attribute:

    add_filter( 'related_posts_by_taxonomy_post_thumbnail_link', 'rpbt_related_post_category_image', 10, 4 );
     
    function rpbt_related_post_category_image( $image, $attr, $related, $args ) {
    	// Get the same image with the size 'medium'.
    	$image = wp_get_attachment_image( $attr['thumbnail_id'], 'medium', false, $attr['describedby'] );
     
    	// Create the link for the image
    	$image = "<a href='{$attr['permalink']}' title='{$attr['title']}' data-toggle='tooltip'>{$image}</a>";
    	return $image;
    }
    

    But the link has lost its title content (it showed up before the filter).
    I guess i have to modify $attr['title']…?

    studioavanti

    (@studioavanti)

    In summary, this code in the filter:
    $image = "<a href='{$attr['permalink']}' title='{$attr['title']}' data-toggle='tooltip'>{$image}</a>";
    Renders this in HTML:
    <a href="https://domain.com" title="" data-toggle="tooltip" data-original-title="">

    So, no title content is rendered and i get a second data-original-title coming from i don’t know where…

    I’d like to get this:
    <a href="https://domain.com" title=My title content" data-toggle="tooltip">

    Any tip please?

    studioavanti

    (@studioavanti)

    My Bootstrap Tooltips finally work, i obtained the related posts title with get_the_title( $post->ID )

    The whole filter code:

    add_filter('related_posts_by_taxonomy_post_thumbnail_link','add_bootstrap_tooltip_to_related_posts_thumbnails', 10,4);
    
    function add_bootstrap_tooltip_to_related_posts_thumbnails( $link, $attr, $post, $args ) {
    	$related_post_title = get_the_title( $post->ID ); 
    	
    	// Link for post thumbnails
    	$link = "<a href='{$attr['permalink']}' title='{$related_post_title}' data-toggle='tooltip'>{$attr['thumbnail']}</a>";
    
    	return $link;
    }
    
    Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi studioavanti

    I’m sorry I’ve missed this post somehow. I’m glad you found a solution yourself 🙂
    I think it sould also work if you used this for the title:

    
    title='{$attr['title_attr']}'
    

    Hi @keesiemeijer,

    No prob, thanks for your hint.
    It seems the option was already available in RPBT and maybe i missed it in the rich plugin documentation: simplest than a custom filter indeed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add data attribute to item link’ is closed to new replies.