• Hey everyone, I am trying to find a way to include the ‘featured image’ (thumbnail) to dump out into the post.

    We hired someone to develop an App and they are using JSON to provide the App with data. We installed a plugin to spit out the JSON and everything is great except with a block of code that fills two major parts of our website – The headlines and top 5 stories. It just dumps out Post_contnet and we really need the image to be include in the JSON of it.

    Here is the main code that generates everything – (Well there is a lot of backend code that provides us with the options of setting if things are sorted by “views”, “shares” and so forth)

    function get_story_list( $m ){
        if( $m->story !== "custom" ){
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'order' => $m->order,
                'orderby' => 'date'
            );
            if( $m->order_by !== "date"){
                $args["meta_key"] = "post_".$m->order_by;
                $args["orderby"] = "meta_value_num";
                $args["date_query"] = array(
                        "after" => '-3 days'
                    );
            }
            if( $m->type == "headline" ){
                $args['posts_per_page'] = '5';
            } else if( $m->type == "list" ){
                $args['posts_per_page'] = '6';
            } else {
                $args['posts_per_page'] = '15';
            }
            if( strlen($m->author) > 0 ){
                $args['author__in'] = explode(",",$m->author);
            }
            if( strlen($m->cats) > 0 ){
                $args['cat'] = $m->cats;
            }
            if( strlen($m->tags) > 0 ){
                $args['tag__in'] = explode(",",$m->tags);
            }
            $query = new WP_Query($args);
            $posts = $query->posts;
        } else {
            $post_ids = explode(",", $m->post_list);
            $posts = array();
            foreach( $post_ids as $p ){
                $posts[] = get_post( $p );
                $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
            }
        }
        return $posts;
    }

    Then it spits out in the template like so –

    <?php
    $stories = get_story_list( $m );
    //var_dump($stories);
    ?>
    
    <div class="module module-list">
    	<?php if( count( $stories ) > 0 ){ ?>
    		<?php
    		$fp = fopen('json/stories.json', 'w');
    		fwrite($fp, json_encode($stories));
    		fclose($fp); ?>
    		<h2><?php echo $m->title; ?></h2>
    		<ul>
    			<?php foreach( $stories as $s ){
    				$category = get_the_category( $s->ID );
    				if( $category[0]->parent !== "0" ){
    					$parent = get_category( $category[0]->parent );
    				} else {
    					$parent = $category[0];
    				}
    
    				$color = get_category_color( $parent->term_id );
    			?>
    				<li class="module-list-tile <?php echo $color.'-h-grad-l-white'; ?>">
    					<div class="wrap">
    						<div class="info">
    							<span class="<?php echo $color.'-font'; ?>"><?php echo $category[0]->name; ?></span>
    							<span class="time">| <?php echo date("M d, G:i", strtotime($s->post_date)); ?></span>
    
    						</div>
    						<h3><?php echo $s->post_title; ?>
    							<div class="author">by
    								<?php $author_tax = wp_get_post_terms( $s->ID, array("authors") );
    								if( count( $author_tax ) > 0 ){
    										echo $author_tax[0]->name;
    								} ?>
    							</div>
    						</h3>
    
    					</div>
    					<a href="<?php echo get_permalink($s->ID); ?>"><span class="span-link <?php echo $color.'-border-m'; ?>"></span><span class="hide"><?php echo $s->post_title; ?></span></a>
    				</li>
    			<?php } ?>
    		</ul>
    	<?php } ?>
    </div>

    And sample of the JSON we are returned –
    `[{“ID”:38719,”post_author”:”1″,”post_date”:”2014-12-08 03:52:38″,”post_date_gmt”:”2014-12-08 12:52:38″,”post_content”:””,”post_title”:”Doug Dubach Moves Ahead”,”post_excerpt”:””,”post_status”:”publish”,”comment_status”:”open”,”ping_status”:”closed”,”post_password”:””,”post_name”:”doug-dubach-974″,”to_ping”:””,”pinged”:””,”post_modified”:”2014-12-08 05:24:22″,”post_modified_gmt”:”2014-12-08 14:24:22″,”post_content_filtered”:””,”post_parent”:0,”guid”:”http:\/\/localhost:8888\/dev\/?p=38719″,”menu_order”:0,”post_type”:”post”,”post_mime_type”:””,”comment_count”:”0″,”filter”:”raw”,”shares_facebook”:”0″,”shares_twitter”:”0″,”shares_googleplus”:”0″,”shares_pinterest”:”0″,”shares_email”:”0″,”post_views”:”0″,”}]

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Include Featured image in "the_post"’ is closed to new replies.