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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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(); ?>">
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0