Skip to content
Advertisement

Get first image from last post in WordPress

Can’t get the first picture in the last post (function – get_first_post_image). Where is the mistake? Please help me. Thank you in advance for your help.

JavaScript

Sorry for my bad English.

Advertisement

Answer

I’ve rewritten the function to return the default image if no first image is found. Best way of parsing HTML is DOM parsing, not regex.

JavaScript

You are calling get_first_post_image() with the ID instead of the post content.

change

JavaScript

to

JavaScript

I’ve updated the function above

  • Changed Signature
  • Removed global $post
  • Changed loadHtml()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement