function ft_get_tree($head_id, $family_id) { $aa = array(); if ($head_id) { $married_id = get_page_by_title('Married', '', 'ft_relation_type'); $parent_child_id = get_page_by_title('Parent/Child', '', 'ft_relation_type'); $level_array = array($married_id->ID, $parent_child_id->ID); $aa['name'] = get_post_meta($head_id, 'member_first_name', true); $aa['id'] = $head_id; $thumb = wp_get_attachment_image_src(get_post_thumbnail_id($head_id), 'thumbnail'); $url = $thumb['0']; $aa['surname'] = get_post_meta($head_id, 'member_last_name', true); $aa['photo'] = $url; $aa['id_family'] = $family_id; $aa['sex'] = get_post_meta($head_id, 'member_sex', true); foreach ($level_array as $key => $value) { $args = array('post_type' => 'ft_relation', 'posts_per_page' => -1, 'order' => 'ASC', 'orderby' => 'post_title', 'meta_query' => array('relation' => 'AND', array('key' => 'member1', 'value' => $head_id, 'type' => 'numeric', 'compare' => '='), array('key' => 'relation_type', 'value' => $value, 'type' => 'numeric', 'compare' => '='))); $the_query = new WP_Query($args); if ($the_query->have_posts()) { while ($the_query->have_posts()) { $the_query->the_post(); if ($value == $married_id->ID) { $aaa = new stdClass(); $aaa->id = get_post_meta(get_the_ID(), 'member2', true); $aaa->name = get_post_meta(get_post_meta(get_the_ID(), 'member2', true), 'member_first_name', true); $aa['sex'] = get_post_meta(get_post_meta(get_the_ID(), 'member2', true), 'member_sex', true); $aaa->surname = get_post_meta(get_post_meta(get_the_ID(), 'member2', true), 'member_last_name', true); $aaa->id_family = $family_id; $aaa->locked = "N"; $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(get_post_meta(get_the_ID(), 'member2', true)), 'thumbnail'); $url = $thumb['0']; $aaa->photo = $url; $aa['spouse'][] = $aaa; } else { $aa['children'][] = (object) ft_get_tree(get_post_meta(get_the_ID(), 'member2', true), $family_id); } } wp_reset_postdata(); } } return $aa; } else { return $aa; } }
function write_family_json_file($family_id) { /* create file for family json*/ $main_array->children[] = (object) ft_get_tree(get_post_meta($family_id, 'family_head', true), get_the_ID()); $json = json_encode($main_array); $file = dirname(dirname(__FILE__)) . '/your-main-php-file.php'; $dir = plugin_dir_path($file); $filename = $dir . '/family_data/family_' . $family_id . '.json'; $somecontent = $json; // Let's make sure the file exists and is writable first. // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!($handle = fopen($filename, 'w+'))) { echo "Cannot open file ({$filename})"; die; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ({$filename})"; die; } fclose($handle); }
<?php /* Template Name: Family Tree */ //get_header(); $main_array = new stdClass(); $main_array->children[] = (object) ft_get_tree(get_post_meta(get_the_ID(), 'family_head', true), get_the_ID()); $json = json_encode($main_array); //print_r($main_array); //print_r($json);die; wp_enqueue_script("jquery"); $dir = plugins_url() . '/family-tree/'; ?> <!DOCTYPE html> <!-- Mirrored from www.chehebar.com/new/ by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 25 Aug 2014 11:36:50 GMT --> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <!-- for IE 9 --> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title><?php echo $post->post_title; ?> | <?php echo get_bloginfo('name'); ?> </title>