Skip to content

Instantly share code, notes, and snippets.

@barbareshet
Created January 29, 2017 18:19
Show Gist options
  • Save barbareshet/99de4b64316e35a75fc4b27c7c16d9ab to your computer and use it in GitHub Desktop.
Save barbareshet/99de4b64316e35a75fc4b27c7c16d9ab to your computer and use it in GitHub Desktop.
<?php
function site_news_posts(){
$query_args = array(
'category_name' => 'category_name',//chooose category
'posts_per_page' => 4,//number of posts to show
);
$query = new WP_Query($query_args);
if ($query->have_posts()) {
echo '<div class="row">';
while ($query->have_posts()) {
$query->the_post();
echo '<div class="col-md-3">';
echo '<a href="' . get_permalink() . '">';
the_post_thumbnail('post-thumbnail', '');
echo '<h4 class="pu-news-title">' . get_the_title() . '</h4></a>';
the_excerpt();
echo '<div class="pu-news-meta">';
echo '<span>Posted on: ' . get_the_date() . ' | </span><br><span>';
comments_number();
echo '</span></div>';
echo '</div>';
}
echo '</div>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
}
//create the shortcode
add_shortcode( 'news_posts', 'site_news_posts' );
// echo the_short code
echo do_shortcode("[news_posts]");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment