• Hi community,

    Got lost with quite a simple task. I’ve got cycle running and need to get tag it to get custom field value. Can’t get tag id. Code is below.

    $product_terms = wp_get_object_terms($post->ID, 'post_tag');
    foreach($product_terms as $term){
    	$categories = get_the_category();
    	$category_id_tag = 'post_tag_'.$categories[0]->cat_ID;
    	$tag_title = the_field('title_page', $category_id_tag);
    	echo '<a>slug, 'post_tag').'" title="'.$tag_title.'"> <span>'.$term->name.'</span></a> ??';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • If you’re trying to get Post Tags then there’s no need for the function get_the_category(). You’re getting an array of post tags by using wp_get_object-Terms() so it should look like this:

    $product_terms = wp_get_object_terms( $post->ID, 'post_tag' );
    foreach ( $product_terms as $term ) {
    	$category_id_tag 	= 'post_tag_' . $term->term_id;
    	$tag_title 			= the_field( 'title_page', $category_id_tag );
    	echo '<a href="#" title="' . $tag_title . '"> <span>' . $term->name . '</span></a>';
    }

    There’s also no cat_ID only term_id.

    Thread Starter dorofeevdima

    (@dorofeevdima)

    Thanks, that was very helpful. Just to be precise and for those who will find this post useful. The_field prints out clean html so the right way will be:

    foreach($product_terms as $term){								   	$category_id_tag = 'post_tag_'.$term->term_id;
    echo '<a href="'.get_term_link($term->slug, 'post_tag').'" title="';
    the_field('title_page', $category_id_tag);									echo '"><span>'.$term->name.'</span>
    <span>'.$tag_title.'</span>
    </a> &nbsp;&nbsp;';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get tag id’ is closed to new replies.