As I have mentioned in my previous article that I have upgraded the blog design. Hence when I was developing this theme I have to make some operation and it was to set the featured image for all post.
I have image uploaded for all the posts but those images were not set as an featured image or we can say that featured image feature was not implemented in that theme. But in this theme I have planned to use the same. So I had two options at that time.
So as a being web developer obviously I have choosen the second option and written one function which selects the image for the posts and assign those image as an featured image.
Let me explain in words, have a look at below steps.
1) Select all posts
2) Get first attached images of the posts using get_children() function
3) Use the set_post_thumbnail() function to set the featured image for the posts.
Have a look at below code block for the same, I have created one function which you can place in your function.php of active theme and its done.
Here you may find some scope of improvement in code in terms of coding standards but the reason for this is, function I have not optimized in those terms. Here you can share any improvements in above code so anyone who uses this may have better help from this article.
Avinash Zala currently working at a leading web development company as a Sr. PHP Web Developer. He is the founder of the Xpert Developer. He has completed B.E. in Information Technology. Born and brought up in Ahmedabad. He loves to code and coding is all about passion for him. Follow him on twitter at Avinash Zala & Xpert Developer.
View all posts by: Avinash