I've very nearly finished my new site design - http://DotNeil.com - but there's a problem with the images on 'Recent Posts' area on in the 'extra' section at the bottom of some pages, e.g. the home page. I use a plugin to render the images;
The plugin is used in combination with a WP query (written a while back by someone from another forum) that works in a similar way to the loop. Unlike the loop, the 'recent posts' script will only show the image for each post if the 'proper' Loop pulls up the respective post on the same page. Here's the script;
I'm sure this is down to a problem with the 'recent posts' script not pulling out all the unique identifiers for a post (isn't it just post ID?). If I include the following line of code from a 'Most Commented' script the images appear, but, in the complete opposite to the previous script, this also only works if the 'proper' Loop is not on the same page, e.g. the 'Archives' tab (http://dotneil.com/blog/archives);
Obviously I'm not a PHP expert; I included this line of code from a 'Most Commented' plugin as a bit of trial and error. It suggests something in the definition of the $posts variable is pulling out post information which is relevant to finding the post images, but it also shows that it doesn't work if the Loop is called on the same page.
Does anyone know how I can fix this 'Recent Posts' script so that it always pulls out the images for each post?
I also want to fix the 'Most Commented', 'Recent Comments' and 'Highest Rated' scripts to pull out the post images too, but this may take a little bit more work, i.e. hard coding the post image plugin call into the plugin files. I'm eager to give it a go, though!
I'm having a bit of trouble understanding exactly what your problem is but looking at your second code snippet I don't think you need this bit:
$imgposts = new WP_Query('showposts=5');
I think all you need is this:
$imgposts = $wpdb->get_results("SELECT * FROM $wpdb->posts ORDER BY post_date DESC LIMIT 5");
You may need to change the "DESC" part to "ASC" depending on which way you want them displayed. DESC stands for Descending and ASC stands for Ascending. If that doesn't help, perhaps you can try to explain what exactly you want a bit more. Sorry, maybe it's just me but I just don't quite understand.
Thanks for the suggestion - how would I fit that line in with the complete script that I've posted? I just tried replacing the 'showposts=5' line with it, and WP didn't like it.
I thought this one might be a little confusing - I'll try to simplify my question;
Check the home page, you'll see that the 'Recent Posts' script doesn't pull up the icons/images that belong to those specific posts - it just shows the default post images (the macbookpro keys). However, the 'Facebook Friend Lists' post does have the correct icon show, but only because it is in the Loop further up the page. Here's a screenshot explaining this;
If you now check the Archives page, the 'Recent Posts' script on this page has been given this extra line of code;
$posts = $wpdb->get_results("SELECT ID, post_title, post_date, comment_count FROM $wpdb->posts ORDER BY comment_count DESC");
And you'll see that all the correct icons have been pulled up next to the respective posts. However, if I use this script on the same page as the Loop, the icons all revert to the default (macbookpro keyboard) icons. And here's a screenshot explaining this too;
Hmm... I've been researching the problem and I've come up empty. Everything looks like it should be working. The only thing that's not is the post images. That would lead me to think that it's something to do with the plugin. Have you tried contacting the plugin author. That's the only thing I can think of right now. Sorry if I'm not being a lot of help. It's just that I haven't ever experienced this particular problem and all your code looks right so I'm just as stumped as you are right now.
Yes I made a comment on the authors blog entry on the plugin - think I'll try contacting them directly. I'm just wondering, if you were to write a script from scratch that pulls up the last 3 or 5 posts, how would you do it?
Well that depends. Do you mean completely from scratch (as in, make my own connection to MySQL, have my own seperate query, etc), or by using some of the lower-level functions in WordPress to access the database directly?
Found the fix. I read through the mountains of comments left on the authors post for the plugin, and 'Reid Beels' suggested altering a 'couple of the global calls' at the top of the plugin file (didn't say which exactly).
I've replaced these lines (51 & 52);
global $post, $posts, $wp_version, $wpdb;
global $post, $wp_version, $wpdb, $wp_query;
$posts = $wp_query->posts;
As you've seen in my previous responses, I've been using
$imgposts = new WP_Query('showposts=5');
to pull up the 'Recent Post' images, whereas Reid Beel says that he uses
So I changed my calls to WordPress to use the following;
And if you check the 'Recent Posts' section on my homepage and on the 'Archives' tab, you'll see that all the icons and the commentlinks show! I've been working on this one for ages, so I'm very pleased!