admin管理员组

文章数量:1022712

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?

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?

Share Improve this question edited May 1, 2019 at 11:23 Thomas Tromp asked May 1, 2019 at 9:45 Thomas TrompThomas Tromp 31 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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; 
}

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?

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?

Share Improve this question edited May 1, 2019 at 11:23 Thomas Tromp asked May 1, 2019 at 9:45 Thomas TrompThomas Tromp 31 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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; 
}

本文标签: Echo tag name if post has one of the tags