How can I load an asset based on custom field value?
- This would be outside of the loop, all I can find for this is in the loop related functions, which I'm not familiar with, or what I find are articles related to the Advanced Custom Fields plugin.
I would like something like this... which I think is most logical for what I am trying to do, but obviously this doesn't exist, at least in this format. But the code below itself should help clarify what I am trying to if my description doesn't.
if ( custom_field( 'field_name', 'field_value' ) ) {
wp_enqueue_style( 'my-custom-style-here' );
}
The closest article I can find is @
But since I don't know the loop it just throws me off because I don't see where I would place my custom field value, not just the custom field name.
I want to do this for performance reasons, so I can load some layout css only if that layout is actually used.
How can I achieve this?
How can I load an asset based on custom field value?
- This would be outside of the loop, all I can find for this is in the loop related functions, which I'm not familiar with, or what I find are articles related to the Advanced Custom Fields plugin.
I would like something like this... which I think is most logical for what I am trying to do, but obviously this doesn't exist, at least in this format. But the code below itself should help clarify what I am trying to if my description doesn't.
if ( custom_field( 'field_name', 'field_value' ) ) {
wp_enqueue_style( 'my-custom-style-here' );
}
The closest article I can find is @ http://www.wpbeginner/wp-tutorials/wordpress-custom-fields-101-tips-tricks-and-hacks/#highlighter_361592
But since I don't know the loop it just throws me off because I don't see where I would place my custom field value, not just the custom field name.
I want to do this for performance reasons, so I can load some layout css only if that layout is actually used.
How can I achieve this?
Share Improve this question edited Jan 19, 2021 at 7:23 Glorfindel 6093 gold badges10 silver badges18 bronze badges asked Feb 10, 2016 at 19:33 Fearless ModeFearless Mode 991 silver badge9 bronze badges 5 |1 Answer
Reset to default 0You would want to use the wp_enqueue_scripts
action, and within there, instead of custom_field
you would use get_post_meta
, like this:
function wpse217823_enqueue_per_custom_field(){
global $post;
$layout = get_post_meta( $post->ID, 'layout', true );
if( $layout == ... ){
wp_enqueue_style( 'my-custom-style-here' );
}
}
add_action( 'wp_enqueue_scripts', 'wpse217823_enqueue_per_custom_field' );
wp_enqueue_scripts
action, and within there, instead ofcustom_field
you would doglobal $post; $layout = get_post_meta( $post->ID, 'layout', true ); if( $layout == ... )
– czerspalace Commented Feb 10, 2016 at 19:53custom_field()
function. – Sisir Commented Feb 10, 2016 at 19:59