我正试图在我的wordpress网站上根据用户输入构建一个谷歌饼图。因此,我创建了自定义元框,并使用wp\\u localize\\u脚本。下面是我为将自定义metabox值传递给wordpress所做的工作。
function pie_load_scripts() {
global $post;
$title = get_post_meta($post->ID, \'title\', true);
$telecom = get_post_meta($post->ID, \'telecommunications-it\', true);
$business = get_post_meta($post->ID, \'business-professional\', true);
$retail = get_post_meta($post->ID, \'retail\', true);
$other = get_post_meta($post->ID, \'other\', true);
$media = get_post_meta($post->ID, \'media-entertainment\', true);
$financial = get_post_meta($post->ID, \'financial\', true);
$government = get_post_meta($post->ID, \'government\', true);
$educational = get_post_meta($post->ID, \'educational\', true);
wp_enqueue_script(\'pie-script\', plugin_dir_url( __FILE__ ) . \'js/3dpiechart.js\');
wp_localize_script(\'pie-script\', \'pie_script_vars\', array(
\'telecom\' =>$telecom ,
\'title\' =>$title ,
\'business\' =>$business ,
\'retail\' =>$retail ,
\'other\' =>$other ,
\'media\' =>$media ,
\'financial\' =>$financial ,
\'government\' =>$government ,
\'educational\' =>$educational ,
)
);
}
add_action(\'wp_enqueue_scripts\', \'pie_load_scripts\');
这是我的Js文件, function drawChart() {
var data = google.visualization.arrayToDataTable([
[\'Sector\', \'Cost\'],
[\'Telecomunications & Information Technology\', pie_script_vars.telecom],
[\'Business Services & Professional\', pie_script_vars.business],
[\'Retail (head office & storefront)\', pie_script_vars.retail],
[\'Others\', pie_script_vars.other],
[\'Media & Entertainment\', pie_script_vars.media],
[\'Financial Services\', pie_script_vars.financial],
[\'Government\', pie_script_vars.government],
[\'Educational & Institutional\', pie_script_vars.educational]
]);
var options = {
is3D: true,
title: pie_script_vars.title
};
以上代码是对google开发者网站的修改(https://developers.google.com/chart/interactive/docs/gallery/piechart?hl=fr) . 在这里,我还想从元框中设置标题的值,这很好,但在饼图值的情况下,它不起作用。