Created
April 2, 2015 14:10
-
-
Save jayshreehcl/920acc311c7a0273ee2d to your computer and use it in GitHub Desktop.
Code snippet to show recent posts from category in wordpress. For more information on how to use it properly and other alternatives please refer to : http://wpvkp.com/show-latest-posts-from-category/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function cust_post() { | |
$check = new WP_Query( array( 'category_name' => 'nameit', 'posts_per_page' => 5 ) ); | |
if ( $check->have_posts() ) { | |
// To enable the theme's default style for list elements | |
$string .= '<ul class="cust_post widget_recent_entries">'; | |
// Checks if the post is present in the category | |
while ( $check->have_posts() ) { | |
$check->the_post(); | |
// Condition to check if the featured image is available | |
if ( has_post_thumbnail() ) { | |
$string .= '<li>'; | |
$string .= '<a rel="nofollow" href="' . get_the_permalink() .'" target="_blank" rel="bookmark">' . get_the_title() .'</a></li>'; | |
} | |
} | |
} else { | |
// If no post is present in the category | |
} | |
$string .= '</ul>'; | |
return $string; | |
wp_reset_postdata(); | |
} | |
add_shortcode('custp', 'cust_post'); | |
add_filter('widget_text', 'do_shortcode'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment