• I’ve written a controller “timeline” like this :

    class JSON_API_Timeline_Controller {
    
        public function category_posts() {
            global $json_api;
            $json = array();
    
            // get attributes
            $category_id = $json_api->query->category_id;
            $post_type = $json_api->query->post_type;
            $amount = $json_api->query->amount;
            $main_post_id = $json_api->query->main_post_id;
    
            if(!$post_type) $post_type = 'post';
            if(!$amount) $amount = -1;
    
            $posts = get_posts(array('post_type' => $post_type, 'numberposts' => $amount, 'category' => $category_id, 'orderby' => $eventdate, 'order' => 'DESC'));
    
            if($main_post_id) $main_post = get_post($main_post_id);
            else  {
                $main_post = $posts[0];
                unset($posts[0]);
            }
    
    if($main_post) {
    
        // setting first (main) post
    
        $json['timeline'] = array();
    
        $json['timeline']['headline'] = $main_post->post_title;
        $json['timeline']['type'] = 'default';
        $json['timeline']['startDate'] = date('Y,m,d', strtotime($main_post->post_date));
        $json['timeline']['text'] = $main_post->post_excerpt;
    
        $json['timeline']['events'] = array();
    
        // example of media asset using the post thumbnail
        if(has_post_thumbnail($main_post->ID)) {
    
            $thumbnail_id = get_post_thumbnail_id($main_post->ID);
            $thumbnail_src = wp_get_attachment_image_src($thumbnail_id, 'large');
            $json['timeline']['events']['media'] = $thumbnail_src[0];
    
        }
    
        if($posts) {
            //$json['timeline']['events']['date'] = array();
            $json['timeline']['events'][$i]['text'] = array();
            $i = 0;
            foreach($posts as $post) {
    
                $json['timeline']['events'][$i]['startDate'] = date_i18n('Y,m,d', strtotime(get_post_meta($post->ID, "_datepicker", true)));
                $json['timeline']['events'][$i]['endDate'] = date_i18n('Y,m,d', strtotime(get_post_meta($post->ID, "_datepicker", true)));
                if ( has_post_format( 'aside',$post->ID ) ) {
                $json['timeline']['events'][$i]['text']['headline'] = $post->post_title;
                $json['timeline']['events'][$i]['text']['text'] = '<span class="timelinelieu">'.get_post_meta($post->ID, "event_lieu", true).'</span>'.$post->post_content;
                } else {
                $json['timeline']['events'][$i]['text']['headline'] = '<a href="'.get_permalink($post->ID).'">'.$post->post_title.'</a>';
                $typelist = '';
                if ($eventtypes = get_field("type_de_event", $post->ID)) {
                    if(is_array($eventtypes)) {
                        foreach ($eventtypes as $key => $eventtype) {
                            $typelist .= $eventtype . ' | ';
                        }
                    $typelist = substr($typelist, 0, -2);
                    $typelist = '<br><span class="timelinetype">'.$typelist.'</span>';
                    } 
                }
                $json['timeline']['events'][$i]['text']['text'] = '<span class="timelinelieu">'.get_post_meta($post->ID, "event_lieu", true).'</span>'.$post->post_excerpt.$typelist;
    
                // example of media asset using the post thumbnail
                if(has_post_thumbnail($post->ID)) {
    
                    $thumbnail_id = get_post_thumbnail_id($post->ID);
                    $thumbnail_src = wp_get_attachment_image_src($thumbnail_id, 'large');
    
                    $json['timeline']['events'][$i]['media']['url'] = $thumbnail_src[0];}
                }
    
                $i++;
    
            }
    
            return $json;
    
                } else return 'Posts not found';
    
            } else return 'Main post not found';
    
        }
    
    } 

    to get a file that could populate a timeline. I need something like this :

    {
    “title”: {
    “media”: {
    “url”: “//www.flickr.com/photos/tm_10001/2310475988/”,
    “caption”: “Whitney Houston performing on her My Love is Your Love Tour in Hamburg.”,
    “credit”: “flickr/tm_10001
    },
    “text”: {
    “headline”: “Whitney Houston<br/> 1963 – 2012”,
    “text”: “<p>Houston’s voice caught the imagination of the world propelling her to superstardom at an early age becoming one of the most awarded performers of our time. This is a look into the amazing heights she achieved and her personal struggles with substance abuse and a tumultuous marriage.</p>”
    }
    },
    “events”: [
    {
    “media”: {
    “url”: “{{ static_url }}/img/examples/houston/family.jpg”,
    “caption”: “Houston’s mother and Gospel singer, Cissy Houston (left) and cousin Dionne Warwick.”,
    “credit”: “Cissy Houston photo:Tom Marcello<br/>Dionne Warwick: CBS Television via Wikimedia Commons
    },
    “start_date”: {
    “month”: “8”,
    “day”: “9”,
    “year”: “1963”
    },
    “text”: {
    “headline”: “A Musical Heritage”,
    “text”: “<p>Born in New Jersey on August 9th, 1963, Houston grew up surrounded by the music business. Her mother is gospel singer Cissy Houston and her cousins are Dee Dee and Dionne Warwick.</p>”
    }
    },
    {
    “media”: {
    “url”: “https://youtu.be/fSrO91XO1Ck&#8221;,
    “caption”: “”,
    “credit”: “Unidisc Music
    },
    “start_date”: {
    “year”: “1978”
    },
    “text”: {
    “headline”: “First Recording”,
    “text”: “At the age of 15 Houston was featured on Michael Zager’s song, Life’s a Party.”
    }
    },

    for now it looks more like this :

    <!– <script> –>
    <!–
    // jQuery(document).ready(function(){
    // jQuery(“#page”).fitVids();
    // });
    –>
    <!– </script> –>
    {
    “status”:”ok”,
    “timeline”:{
    “headline”:”Cie Les Gens De…”,
    “type”:”default”,
    “startDate”:”2017,11,17″,
    “text”:”En ayant la volont\u00e9 de pr\u00e9senter une danse ouverte sur d\u2019autres …”,
    “events”:{
    “”:{
    “text”:[]},
    “0”:{
    “startDate”:”2018,02,15″,
    “endDate”:”2018,02,15″,
    “text”:{
    “headline”:”G\u00e9rard Diby”,
    “text”:”<span class=\”timelinelieu\”>Bures sur Yvette (91)<\/span>Danseur soliste<span class=\”timelinetype\”>CO2NEXIONS <\/span>”},
    “media”:{“url”:”http:\/\/conexion.cluster003.ovh.net\/blog\/wp-content\/uploads\/medias\/2017\/11\/gerard.jpg”}},
    “1”:{
    “startDate”:”2018,02,15″,
    “endDate”:”2018,02,15″,
    “text”:{
    “headline”:”Ludovic Ilolo aka Ucka D\u00e9lia<\/a>”,
    “text”:”<span class=\”timelinelieu\”>Bures sur Yvette (91)<\/span>Com\u00e9dien, Danseur, Chanteur, Chor\u00e9graphe, R\u00e9petiteur… Cie Les Gens De…<br><span class=\”timelinetype\”>CO2NEXIONS <\/span>”},
    “media”:{“url”:”http:\/\/conexion.cluster003.ovh.net\/blog\/wp-content\/uploads\/medias\/2017\/11\/image003.jpg”}},
    “2”:{

  • The topic ‘Json controller for timeline’ is closed to new replies.