最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp admin - How to load an assets based on custom field value?

programmeradmin1浏览0评论

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
  • As i look further, maybe I'm looking for some wrong information. Maybe I'm looking for the "key" and "value"? Is the key equal to the custom field name and the value equal to the custom field value?Reading that out load I think I just answered that. But will dig a little more. – Fearless Mode Commented Feb 10, 2016 at 19:50
  • You would want to use the wp_enqueue_scripts action, and within there, instead of custom_field you would do global $post; $layout = get_post_meta( $post->ID, 'layout', true ); if( $layout == ... ) – czerspalace Commented Feb 10, 2016 at 19:53
  • follow @czerspalace direction. Btw, WordPress doesn't have any custom_field() function. – Sisir Commented Feb 10, 2016 at 19:59
  • @czerspalace thanks for this! Appreciate it. Maybe you can answer the post so I can give it a point or approve the answer? I'm still a bit new to it, but I don't want to answer it myself, because I didn't create the answer. – Fearless Mode Commented Feb 10, 2016 at 23:30
  • @sisir thanks. Yeah, I found that out. Would this be a good idea to have though? It seems more simple and straight forward for cases like this. I not a pro, but if it seems useful that Wordpress could add, I would take the time to submit it. Let me know. Thanks. – Fearless Mode Commented Feb 10, 2016 at 23:31
Add a comment  | 

1 Answer 1

Reset to default 0

You 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' );
发布评论

评论列表(0)

  1. 暂无评论