Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ;?>
share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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? –  TheLoneCuber Apr 17 '12 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 Apr 18 '12 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 ;?>" –  TheLoneCuber Apr 18 '12 at 21:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.