'Adding Alt Text and Title to WP Thumbnail

I'm trying to modify a function to add the alt attribute and title to post thumbnails in WP.

This is the Function code:

    function dl_get_thumbnail($id, $size = 'full'){

    if (has_post_thumbnail( $id ) ){
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), $size );
        $img_url = $image[0];
    } else {
        $img_url = get_stylesheet_directory_uri().'/assets/img/'.$size.'.jpg';  
    }
    
    return $img_url;
}

and this is the code output:

<?php
$class = get_post_type( get_the_ID() ) . ' col-md-4 col-sm-6 result';
?>
<article <?php post_class($class); ?>>
    <?php 
    if( get_field( 'sponsored',get_the_ID() ) ){
        echo '<span class="sponsored">Sponsored</span>';
    }
    ?>
    <div class="result_wrap">
        <header class="<?php echo get_post_type( get_the_ID() ); ?>">
            <p class="post-meta"><?php echo dl_post_meta(get_the_ID()); ?></p>
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        </header>
            <a href="<?php echo the_permalink(); ?>"><img src="<?php echo dl_get_thumbnail(get_the_ID(),'result-large'); ?>" width="320" height="270"></a>
    </div>
</article>

What's the best way to integrate the variables into the function and then output the Alt text and Title value for the thumbnails?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source