-3

I want my link class to be a variable (I am using the filtering system Isotope).

    $categories = get_categories($args);

    foreach($categories as $category) {
        $name = '.'.$category->name;
        echo '<a href="#" data-filter=$name>'; //$name does not work here 
        echo $category->name; 
        echo '</a>';
        echo ' / ';
}

My $name is only displayed as $name in the browser and not the category name I want. Outside the data-filter echoing $name gives all the categories as expected. Is there a way to solve this problem with putting the $name in the data-filter? If the answer is too difficult, please point me to the right direction of what I should do to fix this problem myself please.

Thanks!

deceze
  • 510,633
  • 85
  • 743
  • 889
Folthan
  • 9
  • 5

1 Answers1

1

Strings in single quotes do not do variable expansion. You have to change it to something like this:

echo '<a href="#" data-filter='.$name.'>';

An alternative without the need for concatenation would be:

echo "<a href=\"#\" data-filter=$name>";

Or, more elegant in my eyes:

echo sprintf('<a href="#" data-filter=%s>', $name);

Also I guess you have to add double quotes around that data-filter attributes content...

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks for the clarification about the quotes, I had no idea but I shall remember this now. also the \ before and after the # was necessary like you said. Used your 2nd example. – Folthan May 26 '14 at 08:41