I am working on WordPress website and i have JSON file containing pick-up points data in Russia, placed in WordPress directory "wp-content/uploads/file.json"
and I am trying to fetch cities name from JSON and populate on dropdown using this code:
我在WordPress网站上工作,我有一个JSON文件,其中包含俄罗斯的提货点数据,放在WordPress目录“wp-content/ploads/file.json”中,我正在尝试从JSON中获取城市名称,并使用以下代码填充下拉列表:
$json_data = wp_remote_get('http://1662315-ce34645.twc1.net/wp-content/uploads/cities.json');
// Decode the JSON data
if (is_array($json_data) && !is_wp_error($json_data)) {
$cities = json_decode($json_data['body']);
} else {
echo 'Error fetching JSON data';
}
// Get the city names
$city_names = [];
if (!empty($cities->cities)) {
foreach ($cities->cities as $city) {
$city_names[] = $city->name;
}
}
add_action('woocommerce_after_checkout_shipping_form', 'add_custom_dropdown_after_shipping_form');
function add_custom_dropdown_after_shipping_form() {
// Check if it's the checkout page
if (is_checkout() && !is_wc_endpoint_url()) {
// Create a dropdown of cities
echo '<select id="city-dropdown">';
echo '<option value="">Select a city</option>';
foreach ($city_names as $city_name) {
echo '<option value="' . $city_name . '">' . $city_name . '</option>';
}
echo '</select>';
}
}
I have used many methods to get data like get_content_files
but not working, the main issue is unable to fetch data from JSON. Is there any specific way to place JSON file in WordPress or anything else?
我使用了很多方法来获取数据,比如GET_CONTENT_FILES,但都不起作用,主要问题是无法从JSON获取数据。有没有什么特别的方法可以把JSON文件放在WordPress或其他任何文件中?
更多回答
The issue with your code is that you are trying to fetch the JSON data from an external URL. The wp_remote_get() function is used to fetch data from an external URL, but in your case, the JSON file is located in your WordPress directory.
代码的问题在于您试图从外部URL获取JSON数据。函数的作用是:从外部URL获取数据,但在本例中,JSON文件位于WordPress目录中。
To fetch the JSON data from your WordPress directory, you can use the get_file_contents() function. The get_file_contents() function takes the path to the file as its argument and returns the contents of the file as a string.
要从WordPress目录获取JSON数据,可以使用GET_FILE_CONTENTS()函数。GET_FILE_CONTENTS()函数将文件的路径作为其参数,并以字符串形式返回文件内容。
The following code will fetch the JSON data from your WordPress directory and decode it:
以下代码将从您的WordPress目录中获取JSON数据并对其进行解码:
$json_data = get_file_contents('wp-content/uploads/file.json');
$json_DATA=get_file_contents(‘wp-content/uploads/file.json’);
// Decode the JSON data
//解码JSON数据
$cities = json_decode($json_data);
$CITIES=json_decode($json_data);
Once you have decoded the JSON data, you can get the city names as follows:
一旦您解码了JSON数据,您就可以获得城市名称,如下所示:
// Get the city names
//获取城市名称
$city_names = [];
$city_names = [];
if (!empty($cities->cities)) {
如果(!Empty($Cities->Cities)){
foreach ($cities->cities as $city) {
$city_names[] = $city->name;
}
}
}
The rest of your code is correct, so you should be able to use it to create the dropdown of cities.
代码的其余部分是正确的,因此您应该能够使用它来创建城市的下拉列表。
Here is the complete code:
以下是完整的代码:
cities)) {
foreach ($cities->cities as $city) {
$city_names[] = $city->name;
}
}
add_action('woocommerce_after_checkout_shipping_form', 'add_custom_dropdown_after_shipping_form');
function add_custom_dropdown_after_shipping_form() {
// Check if it's the checkout page
if (is_checkout() && !is_wc_endpoint_url()) {
// Create a dropdown of cities
echo '';
echo 'Select a city';
foreach ($city_names as $city_name) {
echo '' . $city_name . '';
}
echo '';
}
}
?>
更多回答
我是一名优秀的程序员,十分优秀!