Javascript 如何在JSON文件中调用Wordpress get_stylesheet_directory_uri()

Javascript 如何在JSON文件中调用Wordpress get_stylesheet_directory_uri(),javascript,php,json,wordpress,Javascript,Php,Json,Wordpress,首先,我对Javascript毫无希望,对JSON一无所知 我目前正在建立一个网站,并将particles.js整合到设计中。然而,在搜索了几天之后,对于如何将get_stylesheet_directory_uri()函数调用到JSON配置文件中,我感到不知所措 通过在header.php文件中全局声明一个js变量,我能够在particles.js和particles-app.js中找到实现方法,但是对于JSON,我不知道 这就是我需要称之为: "image": { "src":

首先,我对Javascript毫无希望,对JSON一无所知

我目前正在建立一个网站,并将particles.js整合到设计中。然而,在搜索了几天之后,对于如何将get_stylesheet_directory_uri()函数调用到JSON配置文件中,我感到不知所措

通过在header.php文件中全局声明一个js变量,我能够在particles.js和particles-app.js中找到实现方法,但是对于JSON,我不知道

这就是我需要称之为:

  "image": {
    "src": "sprites/blueBlur.png",
    "width": 100,
    "height": 100
  }
如果我保持原样,它只指向根Wordpress文件

整个JSON文件是:

    {
  "particles": {
    "number": {
      "value": 43,
      "density": {
        "enable": true,
        "value_area": 800
      }
    },
    "color": {
      "value": "#00ffd8"
    },
    "shape": {
      "type": "image",
      "stroke": {
        "width": 0,
        "color": "#000"
      },
      "polygon": {
        "nb_sides": 6
      },
      "image": {
        "src": "sprites/blueBlur.png",
        "width": 100,
        "height": 100
      }
    },
    "opacity": {
      "value": 1,
      "random": true,
      "anim": {
        "enable": false,
        "speed": 1,
        "opacity_min": 0.1,
        "sync": false
      }
    },
    "size": {
      "value": 2,
      "random": false,
      "anim": {
        "enable": true,
        "speed": 10,
        "size_min": 40,
        "sync": false
      }
    },
    "line_linked": {
      "enable": false,
      "distance": 200,
      "color": "#ffffff",
      "opacity": 1,
      "width": 2
    },
    "move": {
      "enable": true,
      "speed": 1.9,
      "direction": "none",
      "random": true,
      "straight": false,
      "out_mode": "out",
      "bounce": false,
      "attract": {
        "enable": false,
        "rotateX": 600,
        "rotateY": 1200
      }
    }
  },
  "interactivity": {
    "detect_on": "canvas",
    "events": {
      "onhover": {
        "enable": false,
        "mode": "grab"
      },
      "onclick": {
        "enable": false,
        "mode": "push"
      },
      "resize": true
    },
    "modes": {
      "grab": {
        "distance": 400,
        "line_linked": {
          "opacity": 1
        }
      },
      "bubble": {
        "distance": 400,
        "size": 40,
        "duration": 2,
        "opacity": 8,
        "speed": 3
      },
      "repulse": {
        "distance": 200,
        "duration": 0.4
      },
      "push": {
        "particles_nb": 4
      },
      "remove": {
        "particles_nb": 2
      }
    }
  },
  "retina_detect": true
}
问候
Michael

由于url被分配给头中的全局JS变量,所以您可以像这样在JSON对象中调用/追加它

var url = 'http://example.com/wp-content/themes/twentysixteen';

var json = {
  "image": {
    "src": url + "sprites/blueBlur.png",
    "width": 100,
    "height": 100
  }
};

console.log(json);

您可以通过使用PHP使用样式表的直接url

/wp content/themes/your_theme/style_directory/file.css

这可能会对你有所帮助。

Hi@gvijayan,我已经尝试过了,但是我在Chrome开发工具中发现了这个错误
“Uncaught SyntaxError:JSON中的意外标记v位于位置350,在XMLHttpRequest.xhr.onreadystatechange(particles.js?ver=4.7.5:1530)的JSON.parse()处“
-我已经用整个JSON文件代码更新了原始帖子。@user3436334使用firefox检查并报告输出内容。检查传递给
JSON的参数。parse
是字符串而不是JSON对象。如果是这样,请在字符串化后传递该值。我没有从Firefox获得任何输出,而且它仍然不工作。请检查传递给
JSON.parse的参数,如前一条评论所述。
<?php echo $_SERVER['SERVER_NAME']; ?>/wp-content/themes/your_theme/style_directory/file.css