Forums

Related Post Widget According To Category (1 post)

  1. eternalwarrior1234
    Member
    Posted 2 years ago #

    Given below is the code for related post link below post by category...

    <?php
    $backup = $post;  // backup the current object
    $categories = get_the_category($post->ID);
    if ($categories) {
    	$category_ids = array();
    	foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
    
    	$args=array(
    		'category__in' => $category_ids,
    		'post__not_in' => array($post->ID),
    		'showposts'=>5, // Number of related posts that will be shown.
    		'caller_get_posts'=>1
    	);
    $my_query = new wp_query($args);
    	if( $my_query->have_posts() ) {
    		echo '<h3>Related Posts</h3><ul>';
    		while ($my_query->have_posts()) {
    			$my_query->the_post();
    		?>
    			<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    		<?php
    		}
    		echo '</ul>';
    	}
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>

    The above one will show links from one category. If a post have two categories like SEO, GOOGLE, the code will collect link from SEO category not from the google category. How do I make it obtain link from all categories. Sorry for my ignorance. Any help would be greatly appreciated.

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by eternalwarrior1234
  • This topic is not resolved
  • WordPress version: 3.0