0

Working in WordPress PHP I am trying to pass a value into class tag of an element

<div class="element-item"></div>

to be like

<div class="element-item comedy"></div>

$term = get_the_term_list( get_the_ID(), 'type' );
echo '<div class="element-item '.$term.'">';

the value is pupping out of the class tag and display on the page!

enter image description here

I checked the source code and it seems that I am passing entire a link to the class tag! Canadian

can you please let me know why this is happening and how I can fix it?

2

Right, that's because "get_the_term_list" returns an html string of tags... Those won't work well as class attributes! : )

I suspect what you want is wp_get_post_terms instead:

$terms = wp_get_post_terms( get_the_ID(), 'type', array('fields' => 'slugs') ); // array of term slugs
echo '<div class="element-item'.implode(' ', $terms).'">';

Hope this helps!

0
echo '<div class="element-item '.$term.'">';

Seems like the ' is closing? Have you tried this?

echo '<div class="element-item \'.$term.\'">';

or

echo "<div class=\"element-item $term\">";

https://secure.php.net/manual/en/regexp.reference.escape.php

  • Thanks qworx I tried both ways but not working. the first method just added the .$term. like string to the class and second method is behaving as it was – Behseini Jan 25 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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