WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Related posts (5 posts)

  1. blicht454
    Member
    Posted 1 year ago #

    I'm trying to display related posts based on tags, but I'm running into a few things that just aren't working right.

    Here's the code I'm using:

    <?php global $post;
    					$nextTagThumb='-1';
    					$tags = wp_get_post_tags($post->ID);
    					foreach ($tags as $tag) :
    					?>
    
    					<?php
    					if ($tags) {
    					$what_tag = $tags[($nextTagThumb+'1')]->term_id;
    					$args=array(
    					'tag__in' => array($what_tag),
    					'post__not_in' => array($post->ID),
    					'showposts'=>6,
    					'caller_get_posts'=>1
    					);
    					$my_query = new WP_Query($args);
    					if( $my_query->have_posts() ) {
    					while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    					<!--content goes here-->
    
    					<?php endwhile;
    					}
    					wp_reset_query();
    					$nextTagThumb = ($nextTagThumb+1);
    					}
    					?>
    
    					<?php endforeach; ?>

    The two main problems I'm having are:

    – I can't seem to limit what category it pulls from, which I'd like to be able to do. Otherwise, if I happen to have the same tag in a different category, it connects them, which is not ideal.

    – The 'showposts' line doesn't seem to be working at all. I only want to show the first six related posts, but it shows all of them, no matter what number I enter.

    Any help would be much appreciated.

    You can see the site I'm using this on here: http://www.fiordilattegelato.com
    If you navigate to any single flavor page or single news item, you'll see where the related posts come into play.

    Thanks!

  2. Victor Granic
    Member
    Posted 1 year ago #

    Try 'posts_per_page' instead of showposts.

  3. blicht454
    Member
    Posted 1 year ago #

    Yeah, that's a little better. Still unpredictable though.

    If I have 'posts_per_page'=>1, I'm still getting up to 4 or 5 related thumbs. And I just noticed that sometime I get the same related thumb twice.

    Any ideas?

  4. Victor Granic
    Member
    Posted 1 year ago #

    You could try not using the foreach loop and replace $what_tag and $args with this:

    $what_tag = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    $args = array( 'tag__in' => $what_tag,
                 'post__not_in' => array($post->ID),
                 'posts_per_page' => 6,
                 'ignore_sticky_posts' => 1 );
  5. blicht454
    Member
    Posted 1 year ago #

    That did the trick! Thanks very much! I just added an arg to exclude a category, and now it's working perfectly.

    Here's what worked if anyone else wants to use it:

    <?php global $post;
    $nextTagThumb='-1';
    $tags = wp_get_post_tags($post->ID);
    ?>
    
    <?php
    if ($tags) {
    $what_tag = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    $args = array( 'tag__in' => $what_tag,
                 'post__not_in' => array($post->ID),
                 'posts_per_page' => 6,
                 'ignore_sticky_posts' => 1,
                 'category__not_in' => 9
                 );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
    <!-- CONTENT HERE -->
    
    <?php endwhile;
    }
    wp_reset_query();
    $nextTagThumb = ($nextTagThumb+1);
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic