Skip to content

Instantly share code, notes, and snippets.

@Rubel-hossain
Last active August 29, 2015 14:15
Show Gist options
  • Save Rubel-hossain/cc01147255c0a9038fab to your computer and use it in GitHub Desktop.
Save Rubel-hossain/cc01147255c0a9038fab to your computer and use it in GitHub Desktop.
Query Post by shortcode
<?php
function post_list_shortcode($atts,$content){
extract($atts=shortcode_atts(array(
'type' => 'post',
),$atts) );
$q = new WP_Query(
array('posts_per_page' => '5', 'post_type' => $type)
);
$list = '<ul>';
while($q->have_posts()) : $q->the_post();
$list .= '
<h2> <li><a href="'.get_permalink().'"> '.get_the_title().' </a></li> </h2>
';
endwhile;
$list.= '</ul>';
wp_reset_query();
return $list;
}
add_shortcode('post','post_list_shortcode');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment