Site logo

[Support request] Post title as ALT and TITLE tags for the featured image

Home Forums Support [Support request] Post title as ALT and TITLE tags for the featured image

Home Forums Support Post title as ALT and TITLE tags for the featured image

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2592916
    Melkior

    Hi,
    I have searched here but could not find a solution.
    I need a code (to be placed in functions.php file of my child theme, or some other solution) that does what I have wroted in the topic title.

    I have 200 images (with their own name and alt tag), hat i use as featured image for my 1000 posts. I want to set ALT and TITLE tags for that featured image in a way that it those tags print title of the posts.

    Thank you!

    #2593258
    David
    Staff
    Customer Support

    Hi there,

    can i see your site so i can see how the featured image is being displayed, i can then take a look at what you require.

    #2594024
    Melkior

    Hi,
    the url of the website is in “private information”

    #2594376
    David
    Staff
    Customer Support

    Try this PHP Snippet:

    add_filter( 'wp_get_attachment_image_attributes', function( $atts ) {
        if ( get_post_type( get_the_ID() ) === 'post' ) {
            $atts['alt'] = the_title_attribute( 'echo=0' );
    	$atts['title'] = the_title_attribute( 'echo=0' );
        }
        return $atts;
    } );
    #2594429
    Melkior

    Hi David,
    Great, it works!

    Thank you!

    #2594438
    David
    Staff
    Customer Support

    Glad to hear that!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.