Answer for SEO Query – Image Alt Text replaced with generic text in HMTL

Hello, if you want to get ALT for images, you need add some functions. First please add the following code to the ‘inc/extras.php’ file.

function get_attachment_id_by_url( $url ) {
global $wpdb;

$dir = wp_upload_dir();
$path = $url;

if ( strpos( $path, $dir['baseurl'] . '/' ) === 0 ) {
$path = substr( $path, strlen( $dir['baseurl'] . '/' ) );
}

$sql = $wpdb->prepare(
"SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",
$path
);

$post_id = $wpdb->get_var( $sql );

if ( !empty( $post_id ) ) {
return (int) $post_id;
}
}

Then replace the code like following.

<img class="img-circle" src="<?php echo $service_slide['image']; ?>" alt="service box">
<img class="img-circle" src="<?php echo $service_slide['image']; ?>" alt="<?php echo get_post_meta( get_attachment_id_by_url( $service_slide['image'] ), '_wp_attachment_image_alt', true ); ?>">

Let me know if you need further assistance. Thanks!