Skip to content

Instantly share code, notes, and snippets.

@pebriana
Created June 24, 2015 02:18
Show Gist options
  • Save pebriana/737988c650bc8991fa84 to your computer and use it in GitHub Desktop.
Save pebriana/737988c650bc8991fa84 to your computer and use it in GitHub Desktop.
mengambil 5 post terakir. dalam format json
<?php header('Access-Control-Allow-Origin: *'); ?>
{"berita":[
<?php
$args = array( 'posts_per_page' => 5, 'offset'=> 0 );
$myposts = get_posts( $args );
$no=1;
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
{
"judul": "<?php the_title(); ?>",
"gambar": "<?php if ( has_post_thumbnail() ) {
$x=wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 100,100 ), false, '' );
echo $x[0];
}else{ echo "img-none.jpg"; } ?>",
"deskripsi": "<?php echo str_replace(array("'", "\"", "&quot;","“","”"),"", substr(get_the_content(), 0, 200));?>",
"tautan": "<?php the_permalink(); ?>"
}<?php echo ($no != 5) ? ",":""; $no++; ?>
<?php endforeach;
wp_reset_postdata();?>
]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment