I'm relatively new to Wordpress, and coding in general. I am trying to edit theme-specific recent posts widget to display only posts within a certain category. Currently, the theme will display recent posts with video thumbnails in a category, but it will also include recent posts within any subcategories of that category. I do not want to display posts that appear in subcategories of that category. Can this be done? The code is below.
Widget-home-posts file to categorize:
function widget( $args, $instance ) {
extract( $args );
// If user have not selected any category then display none.
if ( empty( $instance['cat'] ) ) {
return;
}
// Output the theme's $before_widget wrapper.
echo $before_widget;
// Pull the selected category.
$cat_id = $instance['cat'];
// Get the category.
$category = get_category( $cat_id );
// Get the category archive link.
$cat_link = get_category_link( $cat_id );
// Posts query arguments.
$args = array(
'post_type' => 'post',
'posts_per_page' => 3
);
// Limit to category based on user selected tag.
if ( ! empty( $instance['cat'] ) ) {
$args['cat'] = $instance['cat'];
}
// Allow dev to filter the post arguments.
$query = apply_filters( 'videoz_home_recent_args', $args );
// The post query.
$posts = new WP_Query( $query );
Recent posts widget code:
class VideoZ_Recent_Widget extends WP_Widget {
/**
* Sets up the widgets.
*
* @since 1.0.0
*/
function __construct() {
// Set up the widget options.
$widget_options = array(
'classname' => 'widget-videoz-recent posts-thumbnail-widget',
'description' => __( 'Display recent posts with thumbnails.', 'videoz' )
);
// Create the widget.
parent::__construct(
'videoz-recent', // $this->id_base
__( '» Recent Posts Thumbnails', 'videoz' ), // $this->name
$widget_options // $this->widget_options
);
// Flush the transient.
add_action( 'save_post' , array( $this, 'flush_widget_transient' ) );
add_action( 'deleted_post', array( $this, 'flush_widget_transient' ) );
add_action( 'switch_theme', array( $this, 'flush_widget_transient' ) );
}
/**
* Outputs the widget based on the arguments input through the widget controls.
*
* @since 1.0.0
*/
function widget( $args, $instance ) {
extract( $args );
// Output the theme's $before_widget wrapper.
echo $before_widget;
if ( $instance['title'] ) {
echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
}
// Display the recent posts.
if ( false === ( $recent = get_transient( 'videoz_recent_widget_' . $this->id ) ) ) {
// Posts query arguments.
$args = array(
'post_type' => 'post',
'posts_per_page' => $instance['limit']
);
// The post query
$recent = get_posts( $args );
// Store the transient.
set_transient( 'videoz_recent_widget_' . $this->id, $recent );
}
global $post;
if ( $recent ) {
echo '<ul>';
foreach ( $recent as $post ) :
setup_postdata( $post );
echo '<li>';
echo '<a class="entry-thumbnail" href="' . esc_url( get_permalink( $post->ID ) ) . '" rel="bookmark">' . get_the_post_thumbnail( $post->ID, 'videoz-widget', array( 'class' => 'entry-thumb', 'alt' => esc_attr( get_the_title( $post->ID ) ) ) ) . '<span class="play-button"><i class="fa fa-play"></i></span></a>';
echo '<a href="' . esc_url( get_permalink( $post->ID ) ) . '" rel="bookmark">' . esc_attr( get_the_title( $post->ID ) ) . '</a>';
if ( $instance['show_date'] ) :
echo '<time class="entry-date" datetime="' . get_the_date( 'c' ) . '">' . get_the_date() . '</time>';
endif;
echo '</li>';
endforeach;
echo '</ul>';
}
// Reset the query.
wp_reset_postdata();