Skip to content

Instantly share code, notes, and snippets.

@mmh4560
Created August 11, 2017 16:07
Show Gist options
  • Save mmh4560/ff2b491c6728e9f5abc76f8da730da03 to your computer and use it in GitHub Desktop.
Save mmh4560/ff2b491c6728e9f5abc76f8da730da03 to your computer and use it in GitHub Desktop.
Practise advace shortcode and WP_query funtions.
<?php
/*
Shortcode practise
*/
function query_shortcode($attr, $content = null) {
extract( shortcode_atts( array(
'type' => 'post',
'count' => 3,
), $attr));
$args = array(
'post_type' => $type,
'posts_per_page' => $count,
);
$query = new WP_Query($args);
$html_part = '<div class="container">
<div class="row">';
while($query->have_posts()) : $query->the_post();
$id_info = get_the_id();
$html_part .= '<div class="cold-md-4">
<div class="signle-item">
<h2><a href="'.get_permalink().'">'.get_the_title().'</a></h2>
<p>'.get_the_content().'</p>
</div>
</div>';
endwhile;
$html_part .= '</div></div>';
wp_reset_query();
return $html_part;
}
add_shortcode('my_shortcode', 'query_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment