• Resolved dunkkan

    (@dunkkan)


    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi

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

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

    Thread Starter dunkkan

    (@dunkkan)

    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 ?

    Thread Starter dunkkan

    (@dunkkan)

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

    Thread Starter dunkkan

    (@dunkkan)

    <?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.

    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:
    https://codex.www.ads-software.com/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!

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

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_posts won’t show the tags’ is closed to new replies.