Last active
August 29, 2015 14:16
-
-
Save kier0/bb4597de78fa177a4d5a to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment