• Hi,

    I’m trying to format a custom event type based on a custom field.
    I have a list of events where the custom ‘type’ feild can be one of ‘Unavailable Date’, ‘Gig Date’ or ‘Rehearsal Date’.
    I want the post title font to be a different color based on the ‘type’ field.

    This is what i’ve got…

     while($gigsPosts->have_posts()) {
    		$gigsPosts->the_post(); ?>
    
    	
             <li >
    	<?php  $colorOption = get_field('type') ;
    	if( $colorOption = "Unavailable Date"){$color = "red";}
    	elseif ( $colorOption = "Gig Date"){$color = "gray";}
    	elseif ($colorOption = "Rehearsal Date"){$color = "Blue";}
    	?> 
    	    <a href="<?php the_permalink() ?>" >
                  <div class="date">
                    <p class="day"><?php $eventDate = new DateTime(get_field('date'));
    		 echo $eventDate->format('d')?></p>
                    <p class="month"><?php echo $eventDate->format('M')?></p>
                  </div>
                  <div class="item-info-container">
    			
                    <h1 style="color:<?php echo $color; ?>"><?php the_title()?></h1>
                    <p class="item-description">
    					
                      <?php echo get_field('description')?>
                    </p>
                  </div>
                </a>
              </li>
    <?php }
    	?>
    			
      </ul>
          </div>

    With the code as it is, all of my post titles are showing in red.

    If I echo out $color I get ‘red’ for each post (test posts with a mix of ‘type’ categories’.
    If I echo out get_field(‘type’) I get ‘Array’…so presumably the if statement is returning the first array item for each entry?

    any help on this would be great – I’m on the verge of understanding this 😀

    • This topic was modified 4 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 2 replies - 1 through 2 (of 2 total)
  • The first problem is your if statement. You use a single = which means ‘assign a value’, not ‘check equality’. To check for equality, you use == or === for strict comparison.
    Secondly, it would give you more control if you simply put the type on there as a class (but your types have multiple words, sot that wouldn’t quite work without manipulation), and then style the class from the comfort of the Customizer > Additional CSS. That way you can do more than just color.

    Thread Starter Dazzathedrummer

    (@dazzathedrummer)

    Ah – that makes a lot of sense!

    I’ll change my field names to ‘Unavailable’, ‘Gig’ and ‘Rehearsal’.

    I tried with ‘==’ but it wasn’t returning anything – I’ll try again and see if I can spot something odd in what it’s looking up.

    I’m just really learning about CSS (beynd the basics), sounds like a much better idea to determine the class from the post type than using inline CSS.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Format font dynamically – what am I doing wrong?’ is closed to new replies.