You should not hide functionality in a template file. Templates are dumb, you don’t run unit tests on them normally.
I’d recommend to move the logic to a separate function in your functions.php
:
function header_class( $default="white" )
{
$post = get_post();
// 404 pages, empty archives (user, taxonomy, date)
if ( empty( $post ) )
return $default;
$header = get_field( 'header', $post->ID );
if ( empty( $header ) || ! is_scalar( $header ) )
return $default;
return esc_attr( $header );
}
And in your template you can now just call the function:
<header id="masthead" class="site-header <?= header_class() ?>" role="banner">
Related Posts:
- Automatic Shortcode Creation with Custom Fields [closed]
- How can I put a custom field as the link of a button shortcode?
- Display custom meta box in my template file
- Adding number to date not working [closed]
- How can I add extra word in permalink when someone click download button?
- Saving and Restoring a Canvas on A Individual User Basis
- Seach custom post type posts only by meta fields?
- How to display pictures from database?
- Change title only in dynamic page
- How to use thumbnail size of image if I’m only using src to get image
- 3 Slashes appear after Apostrophe in custom fields after updating product-site
- Dynamic content in template
- Populate editor with some content of a page with a page template
- how to retrieve a value if a checkbox is checked
- Trouble checking if custom woocommerce checkout field is empty or not
- Two queries for a WP_User_Query search work perfectly apart, but not together
- How to get the value of input hidden html from text editor to custom page template?
- auto-populating custom nav with all items from custom post type
- Add / Update Custom Fields After Select Pictures in Media Window
- conditional logic for front-end custom field edits
- Trying to retrieve post meta
- Custom field values to taxonomy terms
- How would an if statement surrounding a custom field with two variables (holding values) look like?
- Does wordpress templates always in files or in database?
- How to add specific meta tags to head of cart and checkout pages in woocommerce?
- Custom Field IF/ELSE PHP
- Inject class in body when particular page template is used
- Which PHP page is the Default Posts Page
- How to combine 2 php functions into one function with a custom field
- Remove the last X characters of a custom field value
- Advanced Custom Fields – display label and value only if value entered
- Is it possible to retrieve all posts with a certain value for metadata?
- Adding wrapper elements in the_date() like in the_title()?
- Saving zero in meta box
- Non-Closing PHP Query in WordPress Loop
- Show meta box only for default page template
- WordPress template page name displayed on screen
- Find a way to retrive data updated through metabox plugin to web page
- Why are the details of my todo not saving?
- How to check if a PHP string is different than meta field?
- Can’t save php string to a custom field
- Why does this update_post_meta function not delete the custom field itself?
- How to show/hide php table rows based on the content of custom fields
- Run str_replace on title and save the output to a custom field
- Display all pages – in order
- Only show image from custom field when present
- Editing a PHP file to include a small text piece on every page
- how to get serialized post meta
- Hide Heading if ACF Field is empty
- Custom meta box values are not getting saved for my custom post type
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- Short code template + ajax
- WP_Query: getting posts where custom field exists
- page-slug.php not working but only for specific slug
- How to show meta value code HTML after x paragraph
- Creating an image from a custom field
- User Meta Value not echoing despite Var_Dump Showing correct string
- WordPress loop by meta key that is an array? and how loop multiple arrays
- if custom field doesn’t exist, use post thumbnail instead, as image background
- get_page_template_slug returns incorrect path
- WP All Import / Update stock quantity from multiple XML files
- How to store multiple custom meta box
- Changing the order of custom fields in the dashboard for Woocommerce variable products [closed]
- Dropdown menu for categories
- Handling repeater data
- How to add data to a custom field at the wp_users table?
- Problem with adding custom CSS class to image in ACF Photo Gallery plugin [closed]
- Posts are not looping through correctly
- Concatenate Custom Field Value & HTML Value
- Output custom text field as unordered list
- Conditional featured image with youtube thumbnail
- `update_post_meta` not working anymore
- Order a WP_Query by meta value where the value is an array
- Unable to render custom field after attempt to generate a list of recent post in page template
- Add a custom class to the body tag using custom fields
- Define category ID using get_post_meta
- Add a counter for mouseovers (custom field)
- Hide a div when a custom field is empty
- Make a page (url) not cacheable [closed]
- Including Custom fields within the_content
- How can i make a search box to search by custom field
- Comparing Dates from custom field
- Conditional On custom field plugin metabox
- Show field if it has contents on Advanced Custom Fields
- Adding custom url to readmore link using get_permalink()
- Filter unique custom field value based on custom taxonomy category or other custom field
- the_meta – no get_the_meta query?
- Display movies that apear this day next
- Add “alt” Attribute to GD Star Rating Tags
- Display background color or image with custom meta box?
- Display Custom Field in Sidebar if Value is Present
- custom header text
- Display additional page templates and a sidebar on plugin activation
- Using a string from a custom field within a link
- Meta boxes only displayed when editing normal (default wp post_type) posts
- How do I add my PHP app to a WordPress page whilst keeping semantic URLs?
- WordPress stripping out custom field tags
- Can ‘Custom Field’ data be inserted into this Short Code?
- Using an “IF” statement based on the existence of custom field
- Query pulling a single post per month