PHP add class to html div

61,239

Solution 1

Try this code :

<div class="to-do to-do-wrap<?php echo get_field('to-do-repeater') ? ' complete' : '' ?>"></div>

Solution 2

<?php

echo "<div class=\"to-do to-do-wrap ";
if(get_field('to-do-repeater'))
{
    echo "complete";
}
echo " \"></div>";

?>
Share:
61,239
Pullapooh
Author by

Pullapooh

Updated on August 08, 2020

Comments

  • Pullapooh
    Pullapooh almost 4 years

    i would like to add a class to my html (.complete) with php:

    if( get_field('to-do-repeater') )
    {
        Add (complete) class to <div class="to-do to-do-wrap"> should be <div class="to-do to-do-wrap complete">
    }
    else
    {
        Do nothing
    }
    
    • Pullapooh
      Pullapooh almost 12 years
      there is already a div code <div class="to-do to-do-wrap"></div> i would like to add a new class name to it whit php called complete. I am using wordpress advanced custom fields true or false ( [link] advancedcustomfields.com/docs/field-types/true-false)
  • Pullapooh
    Pullapooh almost 12 years
    if( get_field('to-do-repeater') ) { Add (complete) class to <div class="to-do to-do-wrap"> should be <div class="to-do to-do-wrap complete"> }
  • Oussama Jilal
    Oussama Jilal almost 12 years
    this is HTML and in it there is a php tag (<?php ?>) . Replace your code with this one I wrote
  • Pullapooh
    Pullapooh almost 12 years
    this is not working it is adding a new div i would like to only add a complete class name to an already existing div : )
  • PriestVallon
    PriestVallon almost 12 years
    You can remove the existing div that has the classes "to-do to-do-wrap" and replace it with my code.
  • Pullapooh
    Pullapooh almost 12 years
    yes it is working super perfectly now thanks so much Yazmat : )
  • Pullapooh
    Pullapooh almost 12 years
    ooh yes that's true now it is working too thanks PriestVallon : )