Skip to content

Instantly share code, notes, and snippets.

@Aziz-Rahman
Last active August 29, 2015 14:14
Show Gist options
  • Save Aziz-Rahman/9ca01878387ba438f81a to your computer and use it in GitHub Desktop.
Save Aziz-Rahman/9ca01878387ba438f81a to your computer and use it in GitHub Desktop.
Sortcodes in Wordpress
<?php
//shortcodes latest post
function latest_post( $atts ) {
extract( shortcode_atts(
array(
'limit' => '5',
'cat_id' => '',
), $atts ) );
$args = array(
'post_type' => 'post',
'posts_per_page'=> $limit,
'cat' => $cat_id,
'post_status' => 'publish'
);
$wp_query = new WP_Query();
$wp_query->query( $args );
if ( $wp_query->have_posts() ) :
while( $wp_query->have_posts() ) :
$wp_query->the_post();
echo '<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">'.the_title().'</a>';
echo '<br>';
endwhile;
endif;
}
add_shortcode( 'latestpost', 'latest_post' );
?>
<?php
//ket :
//Masukan code diatas pada file functions.php atau bisa juga buat file baru kemudian di include di file functions.php
//Cara pemangilan, masukan kode [latestpost limit="5"] pada text editor wordpress ! maka akan menampilkan 5 artikel terbaru.
//limit diambil dari parameter shortcode_atts 'limit'
//Enjoy !
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment