Tagged: dynamic, feature image, thumbnail
-
AuthorPosts
-
October 19, 2015 at 12:40 pm #520873
Hi. I did my best to find a solution, but ended up with nothing. I understand this might not be a case covered by regular support, but I’m still hoping for some tips.
I’ve build my site ages ago, back where there was no Feature Image feature in WP, so I created my own function to do so using add_filter(‘the_content’, ‘add_image_at_top’). This function knew the name of the post and it generated the url for the image and added it to the content of the post when it was displayed.
Now I’m upgrading my site and I’ve choose Enfold theme to do so. It’s amazing. However the feature image is being used all over the place, and I have not used this feature, but I still know which image should be the “feature image” for which post. The images are there on the server, just not in the database.
tl;dr
I am looking for a way to inject the image url to the results of the query, so that the template “thinks” the feature image was already there. I’ve tried different approches with add_filter / add_action to ‘the_content’ and ‘the_post’, but I fear this will not cut it, since most elegant solution when building a template is always checking to see if the feature image is there (has_post_thumbnail) and you most certainly did that. So I need to inject the thumbnail info into the results of the query for every post (WP_Post object?) so that the template and WP in general things it was there to begin with.easiest example
I have an image on my server. Say: someurl.com/image.jpg. None of my posts have the feature-image set. I want the someurl.com/image.jpg to be set as a feature-image for all my posts by using add_filter, so that the template “thinks” it was there to begin with – not in the database, but dynamically while the_loop or page or single is being executed.Any help would be much appreciated.
October 19, 2015 at 1:12 pm #520895Heh. As always – chances of solving the problem on your own increase with desperation and amount of posts published about it on the net. Finally I’ve solved this issue. Here is the code added to the functions.php of the child theme.
function _jdutils_inject_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) { if ( empty( $html ) ) { $post = get_post($post_id); $html = "<img src='someimage.jpg' atl='$post->post_title'>"; } return $html; } add_filter( 'post_thumbnail_html', '_jdutils_inject_thumbnail');
Still I’d love if someone more competent than me could look at it and confirm this is the best way to go about it.
Thank you.October 19, 2015 at 1:19 pm #520900Hi!
Glad you figured it out!
Your code looks fine :)
Let us know if you have any other questions or issues!Regards,
YigitOctober 25, 2015 at 6:59 pm #524459The code works, but it’s a very “naive” solution. There are two main problems that I have with them:
1. I have some conditions which define if the injection should happen or not. When I do not want to inject anything I’d like it to behave like it normally does without the add_filter (so what your theme provides for posts with no feature image). Unfortunately when I simply return empty $html from my function it causes a blank result, which is different from theme’s default (icon representing type of post. I’d like this to be as if there was no add_filter when I don’t want to inject anything. I tried remove_filter, but since that happens from withing the function after checking is done it changes nothing.
2. When I inject $html with the tag than no matter what I add as alt or title in the , the template always takes part of the content (usually excerpt) as the alt/title parameter. I think it has something to do with the <span> tag that the is wrapped in which to my understanding is causing the hover effect.And the hover effect leads me to another, less urgent problem, but still important – how do I turn it off and get rid of the link (which points to the image file itself in a lightbox) when viewing the post (is)singular / is_single / is_page)?
-
AuthorPosts
- The topic ‘Inject Feature Image’ is closed to new replies.