我正在尝试从中加载json数据url 我想为json文件中可用的每个英雄创建一个页面。我应该如何检索这个json文件并在多个页面或url中分发内容?
这些页面将具有相同的外观,其中每个英雄的数据都不同。所以,我应该为所有这些文件使用相同的模板吗?我应该如何放置每个单独的数据,如个人简历、姓名和。。。在它的html标记中?
有可能在wordpress中完成这样的任务吗?
我正在尝试从中加载json数据url 我想为json文件中可用的每个英雄创建一个页面。我应该如何检索这个json文件并在多个页面或url中分发内容?
这些页面将具有相同的外观,其中每个英雄的数据都不同。所以,我应该为所有这些文件使用相同的模板吗?我应该如何放置每个单独的数据,如个人简历、姓名和。。。在它的html标记中?
有可能在wordpress中完成这样的任务吗?
是的,可以循环使用json并为每个hero条目创建一个页面。这将为每个人创建一个新页面(在内容区域中添加名称作为标题和简历)。如果需要,您可以创建自己的自定义帖子类型,这将使自定义更加容易。https://codex.wordpress.org/Post_Types
jQuery
$(document).ready(function(){
//Get Jason Data- this is for a local file
$.getJSON(\'/hero.json\',function(data) {
// the action name refers to the php wp_ajax_ action
$.post(\'/wp-admin/admin-ajax.php\',{data:data,action:"add_pages"}, function(response) {
//Check teh Ajax Response
console.log(\'Got this from the server: \' + response);
});
});
});
功能。phpfunction add_pages_function() {
global $wpdb;
// this is how you get access to the database
$data = $_POST[\'data\'];
//loop through json
foreach($data as $d){
echo $d[\'name\'];
//Add pages
$post["id"] = wp_insert_post( array(
"post_title" => $d[\'name\'],
"post_author" => 1,
"post_content" => $d[\'bio\'],
"post_type" => \'page\', //or add \'custom_post_type\' slug
"post_status" => "publish"
));
//UPDATED - for adding meta data (linked custom fields)
update_post_meta( $post["id"],\'my-custom-meta\' , $d[\'super power\'] );
}
wp_die();
}
// uses \'add_pages\' from the action after the wp_ajax
add_action( \'wp_ajax_add_pages\', \'add_pages_function\' );
有关更多wp\\U inerst\\U post选项https://developer.wordpress.org/reference/functions/wp_insert_post/这没有签入以检查帖子标题是否存在,但这应该是一个良好的开始
我想通过ajax post方法检索具有特定ID的内容。例如,如果单击具有特定帖子ID的链接标记,则该ID的内容将加载到post-data 部门。我对wp-ajax非常陌生,有人能帮我做到这一点吗?这是我的代码HTML a tag <a id=\"<?php the_ID(); ?>\" href=\"#\">This is link</a> <!-- the content --> <div id=\"post-data\"&g