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?
 
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?
 
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?!
 
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