• Resolved cogmios

    (@cogmios)


    I have a widget that has a text field. In that text field the user can add a list of tag names e.g. “cat, dog, planet, green”.

    I now want to convert this list of tag names to a string of tag id’s.

    So the output should be e.g. “34,65,22,76”

    What function should I use? Should I use get_term_by ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo $tag->term_id . ‘, ‘;
    }
    }
    ?>

    Thread Starter cogmios

    (@cogmios)

    Well… get_the_tags must be used within the loop.

    I have it working, this is the main part of the widget:

    /** @see WP_Widget::widget */
        function widget($args, $instance) {
            extract( $args );
    
    		$title = apply_filters('widget_title', $instance['title']);
    		echo $before_widget; if ( $title )
            echo $before_title . $title . $after_title;
            $title = apply_filters('widget_title', $instance['title']);
    
    		// get a list of ids and returns a string with term names
    		$text=$instance['text'];
    		$term_id_string1 = '';
    		$term_id_string2 = '';
    		$tag_names = explode(",", $text);
    		foreach ($tag_names as $tag_name)
    		{
    			$aTag = get_term_by( 'name', trim($tag_name), 'post_tag' );
    
    			if ($aTag)
    			{
    				$term_id_string2 .= '<a href="/about/' . trim($tag_name) . '">' . trim($tag_name) . '</a> (' . $aTag->count . '), ';
    				$term_id_string1 .= $aTag->term_id . ',';
    			}
    		}
    		//remove last comma
    		$term_id_string1 = substr($term_id_string1,0,-1);
    		$term_id_string2 = substr($term_id_string2,0,-2);
    
    		// using simple tags
    		//st_tag_cloud('include='.$term_id_string.'&color=false&size=false&xformat=<a href="%tag_link%" class="tag-link-%tag_id%" title="%tag_count% topics" %tag_rel% style="%tag_size% %tag_color%">%tag_name%</a> (%tag_count%)');
    
    		// using our own tag list
    		echo $term_id_string2;
    
    		echo $after_widget;
        }

    (using simple tags plugin or your own)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get Tag Name by ID’ is closed to new replies.