Home › Forums › Front-end Issues › Using the Image ID returns Title as Home
Hello everyone, Im stomped. I’m having a repeater and want to get Images, so far so easy. Now I just realised that every Image has the Title Home (the Images have completely different Titles and alts, is this a Syntax Error or should I rather switch to Array (I loved to have the Srcset though). Appreciate every help! Here is the Code I use.
<?php
if( have_rows('kunden') ):
while( have_rows('kunden') ) : the_row();
$image = get_sub_field('bild');
$size = 'thumbnail';
if( $image ) {
echo "<div class='medium-3 columns'>";
echo wp_get_attachment_image( $image, $size );
echo "</div>";
}
endwhile;
else :
endif;
?>
What is your bild
subfield returning? Are you able to share the output of the code? Are you referring to the title
attribute of the image?
It returns the image with srcset and title=”home”. And yes the Title Attribute. Alt attribute is working, but title is a must for my client.
<img width="400" height="400" src="***/wp-content/uploads/General-Motors.jpg" class="attachment-thumbnail size-thumbnail" alt="General Motors" loading="lazy" srcset="***/wp-content/uploads/General-Motors.jpg 400w, ***/wp-content/uploads/General-Motors-300x300.jpg 300w" sizes="(max-width: 639px) 98vw, (max-width: 1199px) 64vw, 400px" title="Home">
I censored the URL it’s https:// if that makes any difference (which I guess it doesn’t)
Since my client is pressuring me a lot, I ditched srcset temporary to get a proper title.
<?php
if( have_rows('kunden') ):
while( have_rows('kunden') ) : the_row(); ?>
<?php
$image = get_sub_field('bild');
$size = 'thumbnail';
$thumb = $image['sizes'][ $size ];
if( !empty( $image ) ): ?>
<div class="medium-3 columns">
<img />" alt="<?php echo esc_attr($image['alt']); ?>" title="<?php echo esc_attr($image['title']); ?>" />
</div>
<?php endif; ?>
<?php endwhile;
else :
endif;
?>
Since wp_get_attachment
doesn’t return the right img title by default, I tried workarounds. The ones I found where messing up the site or didn’t work at all and my own didn’t work as well.
After rethinking the title attribute, I just decided to get rid of it.
Used
add_filter('wp_get_attachment_link', 'opt_remove_title_attr');
add_filter('wp_get_attachment_image', 'opt_remove_title_attr');
add_filter('wp_nav_menu', 'opt_remove_title_attr');
add_filter('wp_page_menu', 'opt_remove_title_attr');
add_filter('wp_list_categories', 'opt_remove_title_attr');
add_filter('wp_list_pages', 'opt_remove_title_attr');
add_filter('wp_get_archives', 'opt_remove_title_attr');
add_filter('get_archives_link', 'opt_remove_title_attr');
add_filter('post_thumbnail_html', 'opt_remove_title_attr');
function opt_remove_title_attr($return){
return preg_replace('<code>title="(.+)"</code>', '',$return);
}
To get rid of any title and used the code I used before, now it works like a charm, with no title attribute and srcset.
Maybe this helps someone having the same issue.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.