我刚刚在我的wordpress single custom archive页面中添加了一个自定义Highchart。http://propertywise.malisa.agency/en/district/khlong-toei-2/
在Highcharts jQuery(文档)中使用以下函数调用。就绪(函数():
series: [{
type: \'column\',
name : \'<?php echo get_the_title();?>\',
color:"#b6d862",
data: <?php echo average_bedroom_rental_price_for_district($post->ID, \'34\',\'0\', \'district\', $type="DESC" );?>
}]
这很好,并按预期生成图表,但有一点除外。预期的数据数组需要以逗号结尾,例如:
[[0,0],[1,39000],[2,62500],[3,80000],]
不幸的是,我的输出(尽管有效)是:
[[0,0],[1,39000],[2,62500],[3,80000]]
没有结尾的逗号,这会在显示图表后打断页面上的所有jQuery(Google地图标记)。
我已经阅读、研究并尝试了几乎所有我能想到的方法,以不同的方式生成数组,希望获得所需的输出,但没有任何效果,因此,我真的希望有人能够提出建议,纠正我的错误,或者提出从php到jQuery创建数据的最佳方法。
如果它有助于帮助他人回答问题,这是我的php函数:
function average_bedroom_rental_price_for_district($district_id, $contract_type=\'34\',$beds=\'0\', $avg_for, $type="DESC" ){
$numbers_array = array(0,1,2,3,4,5);
$current_array = array();
$average_rental_price_distict_array = array();
$post_id = $id_post;
foreach ($numbers_array as $key)
{
$avg_rental_price_args = array(
\'post_type\' => \'dt_properties\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'contract_type\',
\'field\' => \'id\',
\'terms\' => $contract_type
)
),
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\'=>\'_bedrooms\',
\'value\'=> $key,
\'compare\' => \'=\',
\'type\' => \'numeric\',
),
array(
\'key\'=>\'_property_agency\',
\'value\'=> $district_id,
\'compare\' => \'LIKE\',
)
)
);
$avg_rental_price_query = new WP_Query($avg_rental_price_args);
$total_average_price = 0;
if( $avg_rental_price_query->have_posts() ):
$i=0;
$totalprice = 0;
$pre_price = 0;
while ( $avg_rental_price_query->have_posts() ):
$avg_rental_price_query->the_post();
$the_id = get_the_ID();
$pre_price = get_post_meta ( $the_id, "_property_price",true);
$number_bedrooms = $key;
$totalprice += $pre_price;
$i++;
endwhile;
$total_average_price = ($totalprice/$i);
endif;
$current_array[] = array($key,round_up($total_average_price));
}
$averagePricejson = json_encode($current_array);
return $averagePricejson;
wp_reset_query();
}