2017-09-15 12:39
浏览 94


I have created an ACF option in the admin so a user can add cookie notice text via the admin...The javascript script I'm using creates the message within the javascript so I'm wanting to echo the ACF field within the javascript.

At the top of my cookie.js file I have: "<?php $cooke_msg = the_field('cookie_notice', 'option'); ?>"; and I'm echoing it within a var like so: var msg = "<?php echo $cookie_msg; ?>"; so the top of my file looks like this:

"<?php $cooke_msg = the_field('cookie_notice', 'option'); ?>";


  //Change these values
  var msg = "<?php echo $cookie_msg; ?>";
  var closeBtnMsg = "OK";
  var privacyBtnMsg = "Privacy Policy";
  var privacyLink = "https://www.google.com";

  //check cookies 
   var cookieString = document.cookie;
   var cookieList = cookieString.split(";");
   // if cookie named OKCookie is found, return
   for(x = 0; x < cookieList.length; x++){
     if (cookieList[x].indexOf("OKCookie") != -1){return}; 

What I'm getting when I view the site is: <?php echo $cookie_msg; ?> and not the actual message from ACF...is there a way of actually doing this?

图片转代码服务由CSDN问答提供 功能建议

我在管理员中创建了一个ACF选项,因此用户可以通过管理员添加cookie通知文本... 我正在使用的javascript脚本在javascript中创建消息,所以我想在javascript中回显ACF字段。

在我的cookie.js文件的顶部,我有:< code>“&lt;?php $ cooke_msg = the_field('cookie_notice','option');?&gt;”; ,我在var中回应它: var msg =“&lt ;?php echo $ cookie_msg;?&gt;“; 所以我文件的顶部如下所示:

 ”&lt;?php $ cooke_msg = the_field(  'cookie_notice','option');?&gt;“; 
 var msg =”&lt;?php echo $ cookie_msg;?&gt;“  ; 
 var closeBtnMsg =“OK”; 
 var privacyBtnMsg =“隐私政策”; 
 var privacyLink =“https://www.google.com”; 
 if(文件 .cookie){
 var cookieString = document.cookie; 
 var cookieList = cookieString.split(“;”); 
 //如果名为O的cookie 找到KCookie,返回
 for(x = 0;  x&lt;  cookieList.length;  x ++){
 if(cookieList [x] .indexOf(“OKCookie”)!= -1){return};  

我查看网站时得到的是:&lt;?php echo $ cookie_msg; ?&gt; 而不是来自ACF的实际消息...有没有办法实际执行此操作?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dplsnw7329 2017-09-15 18:33

    Wordpress giving nice function to pass PHP variable data to js file.

    Put this code in your theme functions.php page.

    function mytheme_load_scripts() {
        wp_enqueue_script('mytheme-script', get_template_directory_uri() . '/js/mytheme-script.js');
        wp_localize_script('mytheme-script', 'mytheme_script_vars', array(
            'alert' => get_field("cookie_notice",$post_id)
     add_action('wp_enqueue_scripts', 'mytheme_load_scripts');

    And Create one js folder in your theme root directory. Inside that directory create the js file named mytheme-script.js file, & put the below code over there.

    jQuery(document).ready(function($) {
     alert( mytheme_script_vars.alert );

    Now visit any page of your site. Surely you will get an alert with the your desire field value. Make sure you will assign proper value to $post_id. I think this will help you. Codex reference link for more details: WP Localize Script Function

    解决 无用
    打赏 举报

相关推荐 更多相似问题