How to Display Featured Image Title and ALT Attribute

hi i am try this code for display featured image after first paragraph and display image title and alt attribute add_fil

hi i am try this code for display featured image after first paragraph and display image title and alt attribute

add_filter('the_content', function($content)
{
   $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   $img = '<img src="'.$url.'" alt="" title=""/>';
   $content = preg_replace('#(<p>.*?</p>)#','$1'.$img, $content, 1);
   return $content;
});

featured image display correctly but title and alt attribute not show, please tell me anyone whats a problem in this code

hi i am try this code for display featured image after first paragraph and display image title and alt attribute

add_filter('the_content', function($content)
{
   $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
   $img = '<img src="'.$url.'" alt="" title=""/>';
   $content = preg_replace('#(<p>.*?</p>)#','$1'.$img, $content, 1);
   return $content;
});

featured image display correctly but title and alt attribute not show, please tell me anyone whats a problem in this code

Share Improve this question edited Jun 16, 2018 at 14:37 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Jun 16, 2018 at 13:41 Mohd KashifMohd Kashif 211 silver badge4 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

Maybe following function is what you are looking for.
The important part you are looking for in the code is the line which holds the pathinfo,
which is php and not WordPress specific.

There are probably several other options but as nobody responded till now I think that this function will help you out till another (maybe better) answer is added by someone else.

You could make a backup of the functions.php (found in the theme folder) before you add this function to it.

I have tested it in a sandbox with the WP version 4.9.6 and it should work flawless.

/**
 * Add content on Alt/Title tags
 * 
 * @link    https://wordpress.stackexchange/q/306250
 * @version Wordpress V4.9.6 
 * 
 * Source:
 * @see     https://codex.wordpress/Function_Reference/wp_get_attachment_url
 * @see     https://secure.php/manual/en/function.pathinfo.php
 * @see     https://secure.php/manual/en/function.preg-replace.php
 * 
 * @param   [type] $content [description]
 * @return  [type]          [description]
 */
add_filter( 'the_content', 'add_filename_on_img_tags' );
function add_filename_on_img_tags( $content )
{
    // get featured image
    $url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
    // get filename
    $filename = pathinfo( $arr['name'], PATHINFO_FILENAME );
    // add content on ALT/TITLE tags
    $img = '<img src="' . $url . '" alt="' . $filename . '" title="' . $filename . '"/>';
    // add image after first paragraph
    $content = preg_replace( '#(<p>.*?</p>)#','$1' . $img, $content, 1 );

    return $content;

} // end function

In the docblock you find links with information for code as used in the function.

You have nothing in the code to display those values, try:

$img = '<img src="'.$url.'" alt="'.get_the_title().'" title="'.get_the_title().'"/>';

You can use this as well.

the_post_thumbnail( 'large', array( 'title' => get_the_title(get_post_thumbnail_id()),'alt' => get_the_post_thumbnail_caption() ) );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745118629a4612281.html

相关推荐

  • How to Display Featured Image Title and ALT Attribute

    hi i am try this code for display featured image after first paragraph and display image title and alt attribute add_fil

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信