0

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.

1 Answer 1

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(); ?>">
2
  • thank you very much but just one question get_post_filed('content') gives me the whole content of that post how can i extract just the src attribute of img tag from that as i need that only Nov 12, 2020 at 15:16
  • Well, if the post content already contains an image markup (<img>), you would only need to echo it. E.g. if ( preg_match( '/<img.*?>/', get_post_field( 'post_content' ), $m ) ) echo $m[0];
    – Sally CJ
    Nov 12, 2020 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.