Strange problem with displaying recent posts

Associate
Joined
29 Dec 2004
Posts
2,253
I'm using Wordpress and have been putting bits of PHP together to produce the following:

Code:
<h2>Similar recent articles</h2>
<div class="morecat">
<?php
$categories = get_the_category();
foreach ($categories as $category) :
?>
	<?php
	$thepost = $post;
	$currentpage = $post->ID;
	if ($post->ID == $currentpage) {
	$posts = get_posts('numberposts=6&category='. $category->term_id);
	} else {
	$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=105&amp;h=105&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>

This then displays the "Similar recent articles" section found on this page:

http://richl.com/2008/09/10/apple-release-iphone-21-this-friday/

It has five images and works a treat....on MOST of my pages! And this is the problem, here's an example:

http://richl.com/2007/10/08/sick-of-uk-house-prices/ and another:

http://richl.com/2007/04/28/second-quarterly-update-april-2007/

etc...etc...

I'd say around 80% of my pages are fine, but the rest display this additional image and im not sure why. So far i have echoed $currentpage and $post->ID and on every page they display the correct post ID twice (e.g. 6 and 6, 42 and 42, etc)...

Can anyone help?!
 
Last edited:
Thankyou for your help, i have swapped your code in but am having similar problems, see here:

http://richl.com/2008/07/07/o2-iphone-3g-preorders/
http://richl.com/2008/02/10/new-site-design-for-the-new-year/
http://richl.com/2008/09/10/apple-release-iphone-21-this-friday/

This now has 4 instead of 5 images...but if i go here:

http://richl.com/2007/04/28/second-quarterly-update-april-2007/
http://richl.com/2007/05/01/f-me-ive-added-some-content/

There is 5 images (what it should look like) and im not sure why. I can honestly say i have checked and double checked the pages and there is no difference, apart from the title and the content. The category is the same as they are all in the "News" category.

I really appreciate your help and am more than happy to tweak bits and try things as you suggest them...
 
Last edited:
Yeah i think im also slightly confused which doesn't even begin to help. In summary:

All i want is that block to show 5 other articles to posts in the same category, obviously some of the pages only show 1 or 2 other images because there are only that many posts in their category, but all the pages listed above are in the news category and therefore should have 5 images on each "news" category page!

I think the thing that complicates it is the fact that i don't want to show the current page in that list..if i just say "show the last 5 posts in this category" its simple, but its because i want to say "show the last 5 posts in this category, but make sure you don't show the current post as that person is already on this page"!
 
Can you code me up friend, i think i know what you mean but don't know how to implement it...

I did, by the way, echo $currentpage and $post->ID at one point and they were all correct, both the same number for each...
 
I think i'm sorted, using this code, and it's also random...woo!

Code:
<?php $rand_posts = get_posts('category=' . $category->term_id . '&numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?>
	<div class="morecatimg">
		<img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "Image", true); ?>&amp;w=105&amp;h=105&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" height="105" width="105" />
		<div class="morecattext">
			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
		</div>
	</div>
<?php endforeach; ?>

Thoughts?
 
Last edited:
:D

Well i kept refreshing and it does turn up, but to be honest its random so it's not as likely to happen...could i mod my new code to add your break in?
 
Rightio, this is taking everyone's assistance into account, in in both code and php brackets! Please please tell me if its terrible and if it could be improved, i'm glad its working but i'd love to know if i could make it better etc...

Code:
<h2>Similar recent articles</h2>
<div class="morecat">
<?php 
	$thepost = $post;
	$categories = get_the_category();
	foreach ($categories as $category) 
	{
		$count=0;
		$currentpage = $post->ID;
		if ($post->ID == $currentpage) {
		$posts = get_posts('numberposts=6&orderby=rand&category='. $category->term_id);
		} else {
		$posts = get_posts('numberposts=5&orderby=rand&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=105&amp;h=105&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" height="105" width="105" />
					<div class="morecattext">
					<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
					</div>
				</div>
				<?php
            			$count++;
            			if($count == 5) break;
				}
			}
	 	}
	}
	$post = $thepost; 
?>
</div>


PHP:
<h2>Similar recent articles</h2>
<div class="morecat">
<?php 
	$thepost = $post;
	$categories = get_the_category();
	foreach ($categories as $category) 
	{
		$count=0;
		$currentpage = $post->ID;
		if ($post->ID == $currentpage) {
		$posts = get_posts('numberposts=6&orderby=rand&category='. $category->term_id);
		} else {
		$posts = get_posts('numberposts=5&orderby=rand&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=105&amp;h=105&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" height="105" width="105" />
					<div class="morecattext">
					<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
					</div>
				</div>
				<?php
            			$count++;
            			if($count == 5) break;
				}
			}
	 	}
	}
	$post = $thepost; 
?>
</div>
 
So, i just use something like this?

PHP:
$posts = get_posts('numberposts=6&orderby=rand&category='. $category->term_id . '&exclude=' . $post->ID);
 
Last edited:
It's genius i tells ya, works a treat! I swear that function wasn't there when i looked before! Thanks :)

PHP:
<?php
	$thepost = $post;
	$categories = get_the_category();
	foreach ($categories as $category) 
	{
		$currentpage = $post->ID;
		$posts = get_posts('numberposts=5&orderby=rand&category='. $category->term_id . '&exclude=' . $currentpage);
		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=105&amp;h=105&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" height="105" width="105" />
					<div class="morecattext">
					<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
					</div>
				</div><?php
				}
			}
		}
	}
	$post = $thepost; 
?>
 
Good point sir, is the following alright? Can't get rid of the isset($values) one as it checks to see whether or not there is an image for the post..

PHP:
<?php 
    $thepost = $post; 
    $categories = get_the_category(); 
    foreach ($categories as $category)  
    { 
        $currentpage = $post->ID; 
        $posts = get_posts('numberposts=5&orderby=rand&category='. $category->term_id . '&exclude=' . $currentpage); 
        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=105&amp;h=105&amp;zc=1&amp;q=85" alt="<?php the_title(); ?>" height="105" width="105" /> 
                    <div class="morecattext"> 
                    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> 
                    </div> 
                </div><?php 
            } 
        } 
    } 
    $post = $thepost;  
?>

Thanks again.
 
Back
Top Bottom