Skip to content

Instantly share code, notes, and snippets.

@zainaali
Forked from musamamasood/functions.php
Created September 28, 2018 04:35
Show Gist options
  • Save zainaali/3a5aa3fca0e0e771a50f972f59b0f580 to your computer and use it in GitHub Desktop.
Save zainaali/3a5aa3fca0e0e771a50f972f59b0f580 to your computer and use it in GitHub Desktop.
Shortcode to fetch post by post_type
// create shortcode to list all clothes which come in blue
function statesmen_news_shortcode( $atts ) {
$html = '';
// Attributes
$atts = shortcode_atts(
array(
'post_per_page' => '2',
'post_type' => 'post',
'id' => false
),
$atts,
'statesmen_news'
);
$args = array(
'post_type' => $atts['post_type'],
'posts_per_page' => $atts['post_per_page']
);
if($atts['id']){
$args['post__in'] = explode( ',', $atts['id'] );
}
$get_posts = new WP_Query( $args );
if ( $get_posts->have_posts() ) {
ob_start();
while ( $get_posts->have_posts() ) : $get_posts->the_post(); ?>
<div class="image-div-1" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>">
<div class="mask">
<?php if ( has_post_thumbnail() ) the_post_thumbnail( 'full', array( 'class' => 'mask-img-2' ) ); ?>
<h2><?php the_title(); ?></h2>
</div>
</a>
<div class="clearfix"></div><br>
</div>
<?php endwhile;
wp_reset_postdata();
$html = ob_get_clean();
}
return $html;
}
add_shortcode( 'News', 'statesmen_news_shortcode' );
### <?php echo do_shortcode( "[News id='204,202']" ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment