easier way to swap out thumbnail info? I hope so
-
Hope this is the right forum, if not let me know..
objective: to get a thumbnail to use for facebook meta tags.
specifically, I am trying to make this tag dynamic.
<meta property=”og:image” content=”https://greenspotantiques.com/images/my_fb_logo.jpg”/>I wish to retrieve a thumbnail URL to hand over to Facebook for each post page.
The problem: facebook wants images in a 50 x 50 format.
I’m in luck, I had already defined WP to generate 50×50 thumbs (by default I think?) — so all I need to do is figure out a way to “get” the url to that image.the 50×50 image is NOT used in my theme, but it exists.
So I did the following….. but there’s gotta be a simpler and shorter method.Logic: if it’s a single post page, I want to check if there IS a thumbnail, if there is, I need to read in it’s URL, and parse it out replacing the XX x XX tag with 50×50 and feed it out as a string, only IF the file exists. (in some cases it may not) – in that case, I’d feed out the my_fb_logo.jpg standard website logo…
(I’ve included the img src portion to test result, would be excluded in final).help?
(reason for help? simple, this looks like a pretty hefty call as it would occur every time a page is loaded, in the header…. )
The original url being parsed out is in the form of
“/wp/wp-content/uploads/2010/08/th_GS_8-00889-1-270×225.jpg”if(is_singular()) { //only for single posts or pages $myID = get_post_meta(get_the_ID(), '_thumbnail_id', true); // returns IDentity number of thumb $arr =wp_get_attachment_image_src($myID ); // to get the actual url of thumb if(!empty($arr[0])) { // if empty bail $str = $arr[0]; // first element is the URL, then sizes, etc $myurl= (explode("/",$str)); // splits url into parts $mybasehref= 'https://myserver.com' . $myurl[0] . '/' . $myurl[1] . '/' . $myurl[2] . '/' . $myurl[3] . '/' . $myurl[4] . '/' . $myurl[5] . '/' ; // results in URL without filename $myimg= (explode("-",$myurl[6])); // extracts the filename if(file($mybasehref . $myimg[0] . '-' . $myimg[1] . '-' . $myimg[2] .'-50x50.jpg')) { echo ('<img src="'. $mybasehref . $myimg[0] . '-' . $myimg[1] . '-' . $myimg[2] .'-50x50.jpg' . '">' ); // test with img, to delete in final echo ($mybasehref . $myimg[0] . '-' . $myimg[1] . '-' . $myimg[2] .'-50x50.jpg'); // this would replace the "content" in meta tag for facebook. } } }
the result of the above is
https://myserver.com/wp/wp-content/uploads/2010/08/th_GS_8-00889-1-50×50.jpg
a working URL.
- The topic ‘easier way to swap out thumbnail info? I hope so’ is closed to new replies.