• Resolved rhodian

    (@rhodian)


    Hi guys,

    I’m trying to get related posts based on tags, if no posts are returned then I’m looking to find related posts based on category.

    It appears to be working BUT the last related post displayed has the wrong title, it does have the right permalink however!

    Here is the code I am using:

    <?php
    
    	$backup = $post; //backup current object
    	$current = $post->ID; //current page ID
    
    	global $post;
    	$thisCat = get_the_category();
    	$currentCategory = $thisCat[0]->cat_ID;
    
            $tags = wp_get_post_tags($post->ID);
    
    	if ($tags) {
                $tag_ids = array();
                foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    
                $args=array(
                    'tag__in' => $tag_ids,
                    'posts_per_page'=>3, // Number of related posts to display.
                    'ignore_sticky'=>1,
                    'caller_get_posts'=>1,
                    'numberposts' => 3,
                    'order' => 'DESC',
                    'orderby' => 'ID',
                    'exclude' => $current
                );
    
                $myposts = get_posts($args);
                $check = count($myposts);
    
                if ($check < 1 ) {
                        wp_reset_query();
                        $myposts = get_posts('numberposts=3&order=DESC&orderby=ID&category=' . $currentCategory . '&exclude=' . $current);
                        $check = count($myposts);
                }
            }
    
    	if ($check >= 1 ) { ?>
    	<h1 id="recent">Related</h1>
    	<div id="related" class="group">
    		<ul class="group">
    		<?php
    			foreach($myposts as $post) :
    				setup_postdata($post);
    		?>
    			<li>
    				<a href="<?php the_permalink() ?>" title="<?php the_title() ?>" rel="bookmark">
    					<article>
    						<h1 class="entry-title"><?php the_title() ?></h1>
    						<div class="name-date"><?php the_time('F j, Y'); ?></div>
    						<div class="theExcerpt"><?php the_excerpt(); ?></div>
    					</article>
    				</a>
    			</li>
    
    		<?php endforeach; ?>
    		</ul>
    <?php
    	$post = $backup; //restore current object
    	wp_reset_query();
    ?>
    
    </div><!-- #related -->

    Can anyone help?

    Thanks

Viewing 1 replies (of 1 total)
  • Thread Starter rhodian

    (@rhodian)

    Sorry Guys,

    My stupid mistake… I’d duplicated some posts and not changed the data to reflect the new posts 🙁

Viewing 1 replies (of 1 total)

The topic ‘Incorrect title for related post….’ is closed to new replies.