www.webdeveloper.com
Results 1 to 8 of 8

Thread: WordPress Post Images/Icons

  1. #1
    Join Date
    Dec 2007
    Posts
    40

    WordPress Post Images/Icons

    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;

    http://guff.szub.net/2006/02/09/post-image/

    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;

    Code:
    <?php
    $imgposts = new WP_Query('showposts=3');
    if($imgposts->have_posts()) : while($imgposts->have_posts()) : $imgposts->the_post(); ?>
    	<ul class="recentposts">
    		<li class="recent_post clearfix" id="post-<?php the_ID(); ?>">
    			<div class="posttitle clearfix">
    				<a class="postimagelink" href="<?php the_permalink(); ?>"><img alt="Post icon" class="postimage" src="<?php post_image('shared/images/post_default.png', false, false); ?>" width="35" height="35" /></a>
     				<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    				<p class="meta">
    				Posted <?php the_date('d-m-y'); ?> under <?php the_category(' &amp; ') ?> | <?php comments_popup_link('Comments (0)', 'Comments (1)', 'Comments (%)', 'commentlink', ''); ?> </p>
    			</div>
    		</li>
    	</ul>
    <?php endwhile; endif; ?>
    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);
    Code:
    	<?php
    
    $posts = $wpdb->get_results("SELECT ID, post_title, post_date, comment_count FROM $wpdb->posts ORDER BY comment_count DESC");
    
    	$imgposts = new WP_Query('showposts=5');
    	if($imgposts->have_posts()) : while($imgposts->have_posts()) : $imgposts->the_post(); ?>
    		<ul class="recentposts">
    			<li class="recent_post clearfix" id="post-<?php the_ID(); ?>">
    				<div class="posttitle clearfix">
    					<a class="postimagelink" href="<?php the_permalink(); ?>"><img alt="Post icon" class="postimage" src="<?php post_image('shared/images/post_default.png', false, false); ?>" width="35" height="35" /></a>
    	 				<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    					<p class="meta">
    					Posted <?php the_date('d-m-y'); ?> under <?php the_category(' &amp; ') ?> | <?php comments_popup_link('Comments (0)', 'Comments (1)', 'Comments (%)', 'commentlink', ''); ?> </p>
    				</div>
    			</li>
    		</ul>
    	<?php endwhile; endif; ?>
    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!

  2. #2
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859
    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:
    PHP Code:
    $imgposts = new WP_Query('showposts=5'); 
    I think all you need is this:
    PHP Code:
    $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.

  3. #3
    Join Date
    Dec 2007
    Posts
    40
    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;

    http://dotneil.com/shared/images/icons_theloop.jpg

    If you now check the Archives page, the 'Recent Posts' script on this page has been given this extra line of code;
    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;

    http://dotneil.com/shared/images/icons_archives.jpg

  4. #4
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859
    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.

  5. #5
    Join Date
    Dec 2007
    Posts
    40
    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?

  6. #6
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859
    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?

  7. #7
    Join Date
    Dec 2007
    Posts
    40
    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);
    Code:
    	global $post, $posts, $wp_version, $wpdb;
    	global $post_image_attachments;
    With these;

    Code:
    global $post, $wp_version, $wpdb, $wp_query;
    	global $post_image_attachments;
    
    	if($wp_query->posts!=$posts)
    	$post_image_attachments='';
    
    	$posts = $wp_query->posts;
    As you've seen in my previous responses, I've been using
    Code:
    $imgposts = new WP_Query('showposts=5');
    to pull up the 'Recent Post' images, whereas Reid Beel says that he uses
    Code:
    query_posts()
    So I changed my calls to WordPress to use the following;

    Code:
    <?php query_posts('showposts=3'); ?>
    
    			  <?php while (have_posts()) : the_post(); ?>
    				<ul class="recentposts">
    					<li class="recent_post clearfix" id="post-<?php the_ID(); ?>">
    						<div class="posttitle clearfix">
    							<a class="postimagelink" href="<?php the_permalink(); ?>"><img alt="Post icon" class="postimage" src="<?php post_image('/shared/images/post_default.jpg', false, false); ?>" width="35" height="35" /></a>
    			 				<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    							<p class="meta">
    							Posted <?php the_date('d-m-y'); ?> under <?php the_category(' &amp; ') ?> | <?php comments_popup_link('Comments (0)', 'Comments (1)', 'Comments (%)', 'commentlink', ''); ?> </p>
    						</div>
    					</li>
    				</ul>
    			<?php endwhile; ?>
    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!

  8. #8
    Join Date
    Feb 2003
    Location
    Aberdeen, Washington, USA
    Posts
    1,859
    I'm glad you figured it out! That's great!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles