How do I add post title as alt tag in a post? I tried using array(title => '.the_title.')
but it didnt work. I think I am not suppose to use the_title
. I tired using get_the_title
as well.
Thanks
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this community@its_me got it almost right. Those args must be 2nd resp. 3rd Parameter, not first:
$attr = array(
'title' => 'howdy partner',
'alt' => 'a nice view of the ocean',
);
// pick one, note 2 or 3 arguments:
the_post_thumbnail( 'post-thumbnail', $attr );
echo("<a href='$p->permalink'>". // null for current $post->ID (use within loop)
get_the_post_thumbnail( null, 'post-thumbnail', $attr ).
"</a>");
Try using this <?php the_title(); ?>
Here is example -
<img src="http://link-to-image" title="Image title" alt="<?php the_title(); ?>"/>
the_title_attribute()
codex.wordpress.org/Function_Reference/the_title_attribute
What code does your theme use to show the featured image (aka post-thumbnail)? Most themes do it like this:
<?php the_post_thumbnail(); ?>
or like this (the right way to do it):
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
In that case, replace the code with this:
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail(
array(
'alt' => trim( strip_tags( $attachment->post_title ) ),
);
);
}
?>
...where, 'alt' => trim( strip_tags( $attachment->post_title ) )
tells the the_post_thumbnail
function that it should use the post's title instead of image's alt text.
READ: WordPress Codex — Function Reference for the_post_thumbnail
alt
attribute? Note, there is noalt
tag.