I'm using the below code from WordPress' get_tags Function Reference page

    $tags = get_tags();
    $html = '<div class="post_tags">';
    foreach ($tags as $tag){
          $tag_link = get_tag_link($tag->term_id);      
          $html .= "<a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
          $html .= "{$tag->name}</a>";}
    $html .= '</div>';
    echo $html;

In my functions.php file I have a number of defined constants.

 define('xyz_city','New York');

I need to insert a constant into the tag link title (line 5 of above code). So instead of .....

    title='{$tag->name} Tag'

it needs to be .....

    title='{$tag->name} [insert xyz_city]'

But it's proving beyond my ability. Normally I echo the constants in like this

    <?php echo xyz_city ;?>

1 Answer 1


I think this is what you are going for:

$html .= "<a href='{$tag_link}' title='{$tag->name} {$xyz_city}' class='{$tag->slug}'>";

The {$tag->name} part is a way to insert a function or variable into a "double quoted" string definition. In the case of {$tag->name} it needs the {curly braces} to get the whole object->method thing to work. You can usually leave the curly braces out when you have a double quoted string and want to insert a variable like $xyx_city, but its good practice to put them in there anyway just in case the underscore or something else in the name messes up the string parsing.

  • Thanks for the explanation miahelf. In hindsight, I think I failed to properly explain that the code is in two different files. The get_tags code is in my index.php file, whereas xyz_city is defined in functions.php (like so: define('xyz_city','New York');). So can I reference {$xyz_city} in index.php when it's only defined in functions.php? Commented Apr 17, 2012 at 8:30
  • Hmmm I think I need to see more of the functions.php and index.php code to help you out on that one.
    – miahelf
    Commented Apr 18, 2012 at 0:08
  • The functions.php code is "define('xyz_city','New York');". And that constant gets echoed in to any file with "<?php echo xyz_city ;?>" Commented Apr 18, 2012 at 21:58

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.