Prabin Parajuli Prabin Parajuli - 1 month ago 18
PHP Question

Use hashtag inside the_post_thumbnail() attribute

I am creating Bootstrap Modal appear dynamically on the basis of Thumbnail ID in my WordPress site. However, I need to use

hashtag
# tag inside the
the_post_thumbnail()
attribute. It comments the attribute data instead. My question is


Is there a trick to insert
# tag
inside
PHP
variable ?


Here's my code :

<?php if(has_post_thumbnail()){
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
#var_dump($post_thumbnail_id);
the_post_thumbnail( 'portfolio-thumb', array( 'data-toggle' => 'modal', 'data-target' => get_post_thumbnail_id() ) ); }
?>


in the attribute,
'data-target' => get_post_thumbnail_id()
, I want to display it as
'data-target' => #get_post_thumbnail_id()


Each
data-target
has unique
ID
, that's why I need the hash tag. But I wonder if there's any trick for this. Any help will be appreciated.

Thanks

Answer

Please append the # in you data-target with function.

Try below code :

<?php if(has_post_thumbnail()){ 
    $post_thumbnail_id = get_post_thumbnail_id( $post_id );
    #var_dump($post_thumbnail_id);
    the_post_thumbnail( 'portfolio-thumb', array( 'data-toggle' => 'modal', 'data-target' => "#".get_post_thumbnail_id() ) ); } 
?>