Add CSS to PHP

DerangedWolf

Elite Member
Jr. VIP
Joined
Apr 30, 2018
Messages
1,924
Reaction score
1,512
Hello,

I need a small help. I'm trying to display custom post taxonomy on the front-end in WordPress using this snippet:

PHP:
<?php the_terms( $post->ID, 'origin', 'Origin: ', ', ', ' ' ); ?>

On the front-end, it shows up as Origin: *origin name*

Now, I want to add separate CSS classes to the "Origin:" part and the "origin name" part. How do I do it?

Thank you
 
I'm not quite sure about that, but I think it should work like that:
<p class="YourCustomClassOne">Origin: </p>
<p class="YourCustomClassTwo"><?php the_terms( $post->ID, 'origin', ' ', ', ', ' ' ); ?></p>
You have to try out if the code still works, but normally I wrap the php into an html tag and as you want two different classes on the Elements you have to split it up into two tags, as the "Origin: " is just a normal word and will be echoed from the php, you can easily put it into a paragraph html tag and just get the term with the php.
 
I am not a wp developer but if I understand it correctly you are setting the $before = "Origin:" within the the_terms.
Maybe you could do something like this:

<p class="your_css_class">Origin:
<?php the_terms( $post->ID, 'origin' , ' ' ); ?>
</p>

and define your_css_class in your css file?
 
Depends on what you want to style.

If you want to style a link, I'd use (not tested, but should work):
Code:
<?php $terms = get_the_terms( $post->ID , 'origin' );
   if($terms) {
   foreach ($terms as $term) {
      echo '<a href="' . $term->slug. '" class="YOURCLASS">' . $term->name . '</a>';
   }
}
?>

If you want to have a bit more of a control, go with:
Code:
<span class="YOURCLASS">Origin: <?php the_terms( $post->ID, 'origin'); ?> </span>
 
Back
Top