how to retrieve the image title for image Post Format

Hope everyone is having a good day. in WordPress Codex Post Format the image post format is defined as

image – A single image. The first tag in the post could be considered the image. Alternatively, if the post consists only of a URL, that will be the image URL and the title of the post (post_title) will be the title attribute for the image.

Now i need to know about what title attribute they are talking about in the above definition of image post format and how to retrieve that title through php as all the data is coming from the_content() function, anyone has any idea? i have researched online and there is literally no issue about this one.

Topic post-formats images theme-development Wordpress

Category Web


So first off, I hope you're aware that the quote there is a guideline on how should a post of the image format be displayed to the user, and the thing about the title attribute applies only to posts where the format is image and that the post content is the image URL only, e.g. https://example.com/image.jpg.

what title attribute they are talking about

It's the title attribute of the <img> tag, and the attribute value should be set to the post title.

how to retrieve that title through PHP

There are some functions you can choose from such as get_post_field(), get_the_title() and the_title_attribute().

So for example, your <img> can be generated like so:

<img src="<?php echo esc_url( get_post_field( 'post_content' ) ); ?>"
    title="<?php the_title_attribute(); ?>">

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.