Wordpress 2.6: Using $post breaks comments on single.php

Associate
Joined
29 Dec 2004
Posts
2,253
Hi,

I've been modding my Wordpress theme template (mostly single.php) to show "Other articles from this category", but instead of just text i have made it so it also shows the custom field from the post which is an image.

An example can be seen on my site here, if you look down the page you'll see a list of other recent articles in the same category, note that i'm NOT talking about the "More from this category" section on the right...that bits fine!

http://richl.com/2008/05/20/know-your-limit/

The PHP i used is below, i'm not particularly PHP awesome (;)) so have just been hacking bits together basically, the code i have works but any comments i view for one post will appear on another in the same category! For example, say i have Post A and Post B in Category 1, if i comment on Post A in Category 1 it will also display for Post B...but it wouldn't display in Category 2. I believe it's to do with using $post, as when i rename it to $thepost or similar the comments work, but it then breaks what i want it to do as it will no longer retrieve the_title() or the_permalink() etc.

Here's the code, when i remove this it works fine so it must be the following bit of code:

Code:
		<div class="MoreCat">
		<?php
		$categories = get_the_category();
		foreach ($categories as $category) :
		?>
		<h3>Similar recent articles</h3>
			<?php
			$posts = get_posts('numberposts=5&category='. $category->term_id);
				foreach($posts as $post) :
				$values = get_post_meta($post->ID, "Image", true);
			if (isset($values[0])) { ?>
			<div class="MoreCatImg">
			<img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $values; ?>&amp;w=102&amp;h=102&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" />
			<div class="MoreCatText">
			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
			</div>
			</div>
		<?php } ?>
		<?php endforeach; ?>
		<?php endforeach; ?>
		</div>

Can anyone help?
 
Shot in the dark, but if it is drawing $post to know where to store the comments why not have $thepost = $post at the beginning of all of that and $post=$thepost at the end. Hopefully that should reset $post to whatever it was before your snippet played around with it.
 
That works a treat!

Would you mind explaining why that works and why it happened? I'm trying to figure stuff out for myself so this should help in future :)

Also, any critique for the code above, or is it alright?
 
Unfortunately when it comes to PHP I do the same as you - cobble things together and learn tons of new stuff with each job - so I wont be able to critic your code.

I can tell you why the above works though. Your loop is 'foreach($posts as $post)'. This runs thorough all the post numbers stored in the $posts array; each time setting $post to that particular posts ID. At the end of the loop $post will thus be referencing the last post in the loop. Presumably when it goes to save a comment it saves it with reference to $post - which would normally be that pages post. Because you've changed it to refer to another it saves the comment for that other post. If we save the details of the post of that actual page at the beginning (with $thepost = $post) we can set it back to its original value at the end ($post = $thepost) so everything's referencing the post on-screen correctly. Hope that makes sense.

(I would point out the bottom of your homepage though where it says 'Add thumbnail path to WP2.5 Uploader' :D )
 
Last edited:
If we save the details of the post of that actual page at the beginning (with $thepost = $post) we can set it back to its original value at the end ($post = $thepost)

Ah ok i get it, i love how these things are always so simple...i could stress out for hours doing loads of stuff that isn't that simple!

(I would point out the bottom of your homepage though where it says 'Add thumbnail path to WP2.5 Uploader' )

I'm confused, whats up with it..text too long?!
 
My mistake - I thought that was a blank yet-to-be-filled-in post from the template or something. Clicked on it and it's actually a relatively interesting article!
 
Any ideas on how i can change my code to exclude the current page from my "Related articles"?

It's the same code as above, but i guess id be looking to modify this bit:

Code:
if (isset($values[0]))

Can i do something like

Code:
if (isset($values[0]) & $postid = $currentpage)

Problem is i have no idea how to get the current page ID from within that loop, but what would be the syntax to use if the two above variables both had say a value of '27' (as in, lets pretend i can get the current page ID and just concentrate on the syntax for the if statement...

TIA
 
Hooray for me!

Code:
		<div class="MoreCat">
		<?php
		$categories = get_the_category();
		foreach ($categories as $category) :
		?>
		<h3>Similar recent articles</h3>
			<?php
			$thepost = $post;
			$currentpage = $post->ID;
			$posts = get_posts('numberposts=5&category='. $category->term_id);
				foreach($posts as $post) :
				$values = get_post_meta($post->ID, "Image", true);
			if (isset($values[0])) {
			if ($post->ID !== $currentpage) { ?>
			<div class="MoreCatImg">
			<img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $values; ?>&amp;w=102&amp;h=102&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" />
			<div class="MoreCatText">
			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
			</div>
			</div>
		<?php }
		} ?>
		<?php endforeach; ?>
		<?php endforeach; 
			$post = $thepost; ?>
		</div>

Work's a treat :D

Not sure if im supposed to nestle if statements like that...someone correct me if i'm not!
 
Back
Top Bottom