0

I am trying to load json data from this url and i want to create a page for every single hero available in the json file.how should i retrieve this json file and distribute the content in multiple pages or url's?

the pages would have the same appearance with different data of every single hero in them.so should i use a same template for all of them and how should i put each seperate data like bio, name and ... in it's html tag?

is it possible to do such task in wordpress?

1 Answer 1

1

Yes it is possible to loop through the json and create a page for each hero entry. This will create a new page for each (adds the name as the title and the bio in the content area). You could create your own custom post type if you want, this would make it easier for customisation. 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);
        });
    });

});

Function.php

function 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' );

For more wp_inerst_post options https://developer.wordpress.org/reference/functions/wp_insert_post/

This has no checks in for checking if that post title exists but it should be a good start

4
  • thanks.it works.but i also have another question.how can i add more than one json file and merge them in one page? also why Custom post type is better for doing this task?
    – Hamed
    Nov 4, 2017 at 14:57
  • to merge 2 or more Json objects please look at stackoverflow.com/questions/8478260/… Nov 4, 2017 at 15:40
  • If you create a custom post type you could create specific custom fields linked to that post type where you can insert the Json data into and then display on a custom template. Nov 4, 2017 at 15:49
  • no problem, glad to help. Dont forget to mark the correct answer to your question, so prople who have the same issue in the future can find the solution. Nov 5, 2017 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.