Support » Fixing WordPress » How to display post from certain category?

  • Resolved johhny

    (@johhny)


    Hi,

    I have this widget, basically recent post widget. But I want the widget to display certain posts in certain category. For example,

    Home tab (no widget)
    People tab (people posts only)
    Animal tab (animal posts only)

    here’s the snippet of coding I tried to change;

    $category = get_the_category();
    $current_category = $category[0]->term_id;

    $r = new WP_Query( apply_filters( ‘widget_posts_args’, array( ‘posts_per_page’ => $number, ‘cat’ => $current_category, ‘no_found_rows’ => true, ‘post_status’ => ‘publish’, ‘ignore_sticky_posts’ => true ) ) );

    in original recent widget codes, there are no italic one. please help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Andrew Nevins

    (@anevins)

    This forum does not provide support, try the How-To forum.

    Oh my bad. Sorry.

    Moderator Jan Dembowski

    (@jdembowski)

    Brute Squad and Volunteer Moderator

    Moved to the How-To and Troubleshooting forum. 😉

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Please don’t edit core widget files.
    Are “People” and “Animal” category pages?

    If so, try it with this in your theme’s functions.php:

    add_filter( 'widget_posts_args', 'filter_recent_post_widget' );
    
    function filter_recent_post_widget($args) {
    
    	if(is_category()) {
    		$current_cat = get_query_var('cat');
    		$args['cat'] = $current_cat;
    	}
    
    	if(is_home()) {
    		return '';
    	}
    
    	return $args;
    
    }

    Thanks for your tip keesiemeijer. I manage to get what I wanted. Thank you so much! 😀

    opsss forgot to mark as resolve.

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    You’re welcome. I’m glad you got it resolved 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display post from certain category?’ is closed to new replies.