WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_posts won't show the tags (7 posts)

  1. Dunkkan
    Member
    Posted 5 years ago #

    Hello,

    I'm building a custom theme and in the homepage (index.php), I'm showing 4 posts from different categories.

    All is working OK with get_posts, the title and the content are shown as normal ; only the tags fail to appear.

    If I set up a conventional Loop, the tags show again.

    Here it is one of my loops, the others are 'clons' but changing the category :

    <?php
    	 $posts = get_posts('category=6&numberposts=1');
    	 foreach($posts as $post):
    		setup_postdata($post);
    	 ?>
    	<?php the_title(); ?>
    	<?php the_content(); ?>
    	<?php the_tags(); ?>
    	<?php endforeach; ?>

    I've tried to do the same through WP_query, with no results :

    <?php $my_query = new WP_Query('showposts=1&cat=4'); ?>
    	<?php if (have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<?php the_title(); ?>
    	<?php the_content(); ?>
    	<?php the_tags(); ?>
    	<?php endwhile; endif; ?>

    I have several times worked with both, get_posts and WP_query (even if I ignore their differences, which it could be cool to know), and I don't remember any issue.

    Finally I tried to put a rewind_posts function, which I think it protects several Loops to 'contaminate' each other, but with no difference :

    <?php get_header(); rewind_posts(); ?>

    Thanks for any input if anybody have some idea.

  2. stvwlf
    Member
    Posted 5 years ago #

    Hi

    Check down near the bottom of this thread - this might be useful to you

    http://www.nabble.com/Get-query-variables-and-tags-td21801063.html

  3. Dunkkan
    Member
    Posted 5 years ago #

    Hi stvwlf, thanks for that link, with this other thread, it gave me an idea of what was happening.

    Here's my final code. It seems like I needed to specify the called post's ID to acces to its tags.

    <?php
    		 $posts = get_posts('category=4&numberposts=1');
    		 foreach($posts as $post):
    			setup_postdata($post);
    		 ?>
    		<?php the_title(); ?>
    		<?php the_content(); ?>
    
    		<!-- HERE IT COMES THE TRICKY PART -->
    
    		<?php $posttags = get_the_tags($post->ID);
    		if ($posttags) {
    		foreach($posttags as $tag) {
    		echo $tag->name . ' ';
    		}
    		}
    		?>
    		<?php endforeach; ?>

    It works the same with WP_query.

    By the way, does someone knows the main differences between WP_query and get_posts ? Which is quicker, or better ? why two functions ?

  4. Dunkkan
    Member
    Posted 5 years ago #

    I'm looking now for a way to get the tags linking to its archive pages :/

  5. Dunkkan
    Member
    Posted 5 years ago #

    <?php $posttags = get_the_tags($post->ID); if ($posttags) {
    foreach($posttags as $tag) {
    echo '<a href="';
    echo get_tag_link($tag);
    echo '">';
    echo $tag->name . ' ';
    echo '</a>';
    }
    }
    ?>

    This is working for me, but I'm suspicious 'cause it must be a cleaner mode to get it. Please don't hesitate to make your remarks.

  6. Kate
    Member
    Posted 5 years ago #

    Dunkkan: thanks for the info. Not sure what you meant by "a cleaner mode", because this looks similar to the examples given from WordPress here:
    http://codex.wordpress.org/Template_Tags/get_the_tags

    Anyways, I wanted to be able to have it separated by commas, so I used this:

    <?php
    $posttags = get_the_tags($post->ID);
    if ($posttags) {
    /*Define counter*/
    $counter = 0;
      foreach($posttags as $tag) {
      /*Add to counter*/
      $counter++;
       echo '<a href="';
       echo get_tag_link($tag);
       echo '">';
       echo $tag->name;
       echo '</a>';
       /*If you're at the last tag, do nothing.  Otherwise, add a comma and a space.*/
       if(!($counter < count($posttags))){
        /*Do nothing*/
       }else{
        echo ', ';
       }
      }
    }
    
    ?>

    Hope that helps someone else too! ;) And, thanks again Dunkkan!

  7. alexo05
    Member
    Posted 4 years ago #

    Thanks to you both, exactly what I was looking for!

Topic Closed

This topic has been closed to new replies.

About this Topic