kier0
3/4/2015 - 3:31 AM

Using Advanced Custom Fields for Alternate Styles that can be selected in Post/Page editor. This uses True/False Selection. If the selecti

Using Advanced Custom Fields for Alternate Styles that can be selected in Post/Page editor. This uses True/False Selection. If the selection in ACF is true, this code echos inline css styles.

Change "no_banner_image" to whatever field name you created as an option.

<?php wp_enqueue_script('jquery'); ?>

<?php
if( get_field('no_banner_image') )
{
    echo "
    <style>
     .class-one { 
        background:red
      }
    .class-one h1 { 
        color:purple;
      }
  .class-two { 
        background:blue;
      }
    </style>";
}
else
{
    
}

/*
*  Query posts for a true/false value.
*  This method uses the meta_query param to match the string "1" to the database value "1|0"
*/

$posts = get_posts(array(
	'meta_query' => array(
		array(
			'key' => 'field_name',
			'value' => '1',
			'compare' => '=='
		)
	)
));

if( $posts )
{
	foreach( $posts as $post )
	{
		setup_postdata( $post );

		// ...

	}

	wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
}

?>