Title: Help &#8212; php code to get first tag into variable
Last modified: August 19, 2016

---

# Help — php code to get first tag into variable

 *  [texasag90](https://wordpress.org/support/users/texasag90/)
 * (@texasag90)
 * [17 years, 10 months ago](https://wordpress.org/support/topic/help-php-code-to-get-first-tag-into-variable/)
 * Hello;
 * I am trying to get the text of the first tag into a php variable in a template
   so I can use it for something. I am trying to do this in single.php (Kubrick).
   Code looks like this:
 *     ```
       <?php
                   $category = get_the_category();
                   $tag = get_the_tags();
                   echo $tag[0]->name;
       ?>
       ```
   
 * That does not work (no echo — nothing). But, $category works just fine when used
   in a similar way.
 * I thought I might need to dereference the pointer, but I cannot find a combination
   that works (I am a php hacker, obviously, not a pro).
 * Can anyone tell me what is wrong?
 * Thanks,
    Mark

Viewing 1 replies (of 1 total)

 *  788444
 * [17 years, 10 months ago](https://wordpress.org/support/topic/help-php-code-to-get-first-tag-into-variable/#post-820154)
 * <?php
    $tag = get_the_tags(); if ($tag) { $tag = $tag[0]; echo $tag->name; } ?
   >
 * That gets the information from the array of the first tag. Since its an Array
   it uses the -> selector to get the information. Category will work just fine 
   because you are pulling it directly from the function.
 * That code is pulled directly out of the Codex.
 * [http://codex.wordpress.org/Template_Tags/get_the_tags](http://codex.wordpress.org/Template_Tags/get_the_tags)
 * directly below the code on that page are the different variables you can call.

Viewing 1 replies (of 1 total)

The topic ‘Help — php code to get first tag into variable’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [tags](https://wordpress.org/support/topic-tag/tags/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 1 reply
 * 2 participants
 * Last reply from: 788444
 * Last activity: [17 years, 10 months ago](https://wordpress.org/support/topic/help-php-code-to-get-first-tag-into-variable/#post-820154)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
