How to add Featured Image Alt tag ( wordpress Home page)
Question
I want to set the first picture on the subject as the featured visual.
and I added these codes to content.php.
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail(‘large’);
} else {
echo ‘<img src=”‘;
echo catch_that_image();
echo ‘” alt=”” />’;
} ?>
and I added these codes to function.php
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
if(preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches)){
$first_img = $matches [1] [0];
return $first_img;
}
else {
$first_img = get_template_directory_uri()."/images/default.jpg";
return $first_img;
}
}
but there is a problem. There is no image alt tag.
How can I fix?
my web page: https://www.ceofix.net/
0
2 years
2020-09-27T04:10:32-05:00
2020-09-27T04:10:32-05:00 0 Answers
18 views
0
Leave an answer