0

I'm trying to echo one of the tags the posts has. There are multiple tags, around 40.

Currently I'm trying:

<?php
  $tag = get_tag(16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58);
  echo $tag->name;
?>

Edit:

function misha_filter_function(){


$args = array(
   'orderby' => 'date', // we will sort posts by date
   'order' => $_POST['date'] // ASC или DESC
  );


$args = array(
    'tax_query' => array(
      'relation' => 'AND',
      array(
        'taxonomy' => 'post_tag',
        'field' => $cat_id,
        'terms' => $_POST['locationfilter'],
      ),
    )
  );


$relation = 'AND';
    if( isset( $_POST['timefilter'] ) )
        $args['tax_query'] = array(
            'relation' => $relation,
            array(
                'taxonomy' => 'post_tag',
                'field' => $tag_id,
                'terms' => $_POST['timefilter']
            ),
        );

$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post(); ?>
<!-- post -->
<a href="<?php the_permalink()?>">
  <div class="col-md-3 col-sm-6 ver-item">
    <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
    <div class="thumb" style="background-image:url('<?php echo $thumb['0'];?>');"></div>
    <section>
      <time>

        <!-- here is where the tag should be displayed -->
        <?php $post_tags = get_the_tags('YOUR POST ID');
        if ( $post_tags ) {
          echo $post_tags[0]->name;
        }?>
      </time>

      <h3><?php the_title();?></h3>
      <span><!-- underline --></span>
    </section>
  </div>
</a>

    <?php endwhile;
    wp_reset_postdata(); else :
    echo 'Geen resultaten, probeer het opnieuw.';
endif;
die();

But the problem with this is that it only outputs the first tag on all posts. That's the tag with the id 16. It even echo's the tag-id 16 to other posts, which don't even have that tag.

How can I echo only one the tags, if the posts has that tag/has one of those tag-ids?

1 Answer 1

2

Function get_tag retrieves post tag by tag ID or tag object.
This tags object is not connected with your current post.

Use get_the_tags to retrive tags of a current post in the loop

$post_tags = get_the_tags();
if ( $post_tags ) {
    echo $post_tags[0]->name; 
}

Add post id to get_the_tags function to retrieve tags of this post without loop

$post_tags = get_the_tags('YOUR POST ID');
if ( $post_tags ) {
    echo $post_tags[0]->name; 
}

Check if category exists in array of included tags using in_array

//outside the loop
$date_tags = array(16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58);

//inside the loop
//shows only first tag name if it finds one or more
$post_tags = get_the_tags();
if ( $post_tags && in_array($post_tags[0]->term_id, $date_tags ) ) {
    echo $post_tags[0]->name; 
}
7
  • Hi, thanks for your answer. If I put all the tag-ids in the () behind the get_the_tags, it still doesn't output anything. I have this right now: $post_tags = get_the_tags('16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58'); if ( $post_tags ) { echo $post_tags[0]->name; } Commented May 1, 2019 at 10:25
  • It's still hard to understand the purpose of your code.You have a lot of posts and you want to display only one tag for each post (if post has one). Why are you hardcoding tags' ids?
    – anton
    Commented May 1, 2019 at 10:47
  • Okay, it's because a customer wants that. Each post has a date, which is a tag they can add to a post. There are multiple dates of course. But each post has a different date, so a different tag. That's why there are a lot of tags: tag-ids. Each post has to echo the date(tag) it has. No other tags. There are also other tags which aren't dates, but they have to be ignored. Do you get me? Commented May 1, 2019 at 10:52
  • It's not a best solution to use tags as dates, but ok. I still don't see a reason why you trying to type tags ids manually. Paste your full code with loop, please. If you decided to use tags category as dates and other tags, you can set a tag slugs for dates like 'date-16', 'date-17' and so on. After, If you will decide to show date in one place and other tags in another - you can check if the slug beggins with 'date-'.
    – anton
    Commented May 1, 2019 at 11:05
  • 1
    The $date_tags = ( should be $date_tags = array( ..
    – Sally CJ
    Commented May 1, 2019 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.