WordPress widget to show posts from particular class
Open features.php file in your present theme. Copy the below-given code within the features.php file of your theme.
1 |
// add our shortcodeadd_shortcode('pk_get_posts', 'pk_get_posts');operate pk_get_posts( $atts ) {international $put up;$str = '';extract( shortcode_atts( array('numberposts' => '5','offset' => '','class' => '','order' => 'DESC','embody' => '','exclude' => '','meta_key' => '','meta_value' => '','post_type' => 'put up','post_mime_type' => '','post_parent' => '','post_status' => 'publish',), $atts ) );$args = array('numberposts' => $numberposts,'offset' => $offset,'class'=> $class,'orderby' => $orderby,'order' => $order,'embody' => $embody,'exclude' => $exclude,'meta_key'=> $meta_key,'meta_value' => $meta_value,'post_type' => $post_type,'post_mime_type' => $post_mime_type,'post_parent' => $post_parent,'post_status' => $post_status);$postslist = get_posts( $args );$str = '<div class="recent-article-box">';$str .= '<ul class="recent-article">';foreach( $postslist as $put up ) :setup_postdata($put up);$str .= '<li><a href="'. get_permalink( $post->ID ) .'" title="'. $post->post_title .'">'. $post->post_title .'</a></li>';endforeach;$str .= '</ul>';$str .= '</div>';wp_reset_query();return $str;} |
We use wp_reset_query() features to destroy the earlier question used on a customized loop.
Tips on how to use the shortcode?
Put [pk_get_posts] in your put up, pages or textual content widget.
Observe: The class parameter must be the ID of the class, and never the class title.
That shows all posts in all classes, to restrict put up show on particular class solely, add class parameter.
1 |
[pk_get_posts category="5"]// the place 5 is the class ID |
To restrict put up show use numberposts parameter.
1 |
[pk_get_posts numberposts="15" category="5"]// the place 15 is the variety of posts to show.// the place 5 is the class ID |
So as to add extra parameters use parameters I’ve listed above.
If the shortcode didn’t work within the textual content widget, don’t fear.
Add this quick snippet in features.php of your theme.
1 |
add_filter( 'widget_text', 'do_shortcode' ); |