WordPress.org

Ready to get started?Download WordPress

Forums

How to display posts from same tag as the current one in the sidebar? (7 posts)

  1. Haarball
    Member
    Posted 5 years ago #

    It was hard making a descriptive title for this one. Here's the crux of it:

    I have a page on which I display posts from a certain category. When going to the permanent link for one of these posts, I want a sidebar that displays posts from the same tag as that post (I will only need to assign one tag). Obviously, there is already a loop in the document to display the post.

    I tried something like this, which seems to be the wrong approach:

    `<?php
    $thetag = "single_tag_title();";
    $postslist = get_posts('numberposts=5&orderby=date&tag=$thetag');
    foreach ($postslist as $post) :
    setup_postdata($post); ?>

    bla bla bla

    <?php endforeach; ?>`

  2. MichaelH
    Member
    Posted 5 years ago #

    Something like this

    <?php
    if ( is_single() ) {
      $tags = wp_get_post_tags($post->ID);
        if ($tags) {
        $first_tag = $tags[0]->term_id;
        echo 'first_tag' .$first_tag;
        $args=array(
          'tag__in' => array($first_tag),
          'post__not_in' => array($post->ID),
          'showposts'=>5
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Related Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($tags)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
  3. Haarball
    Member
    Posted 5 years ago #

    Thank you! It seems to work fine, except it outputs the number 9 before the title (9 is the page id for the page on which these posts are located) and 'first_tag' (as opposed to the name of the tag, I guess). The exact out put is this (after I removed 'Related posts'):

    first_tag9 My Title

    It also doesn't output the_excerpt. Is there a way to make that happen?

  4. MichaelH
    Member
    Posted 5 years ago #

    Sorry delete this line:

    echo 'first_tag' .$first_tag;

    add the_excerpt(); just after the line that begins with

    <a href="<?php the_permalink()
  5. Haarball
    Member
    Posted 5 years ago #

    I did that -- nothing shows. the_content works, but I can't use that.

    I encounter this sometimes; the_content working when the_excerpt won't.

  6. MichaelH
    Member
    Posted 5 years ago #

    Works okay for me. Of course displaying all that excerpt in the sidebar looks ugly, but it works.

    Actually place it:

    like this

    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
           the_excerpt();
          endwhile;
  7. Haarball
    Member
    Posted 5 years ago #

    Yeah, doesn't work for me. Must be something in my code interfering; after all, it is highly modified.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags