WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display post from certain category? (8 posts)

  1. johhny
    Member
    Posted 1 year ago #

    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.

  2. Andrew
    Forum Moderator
    Posted 1 year ago #

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

  3. johhny
    Member
    Posted 1 year ago #

    Oh my bad. Sorry.

  4. Moved to the How-To and Troubleshooting forum. ;)

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    
    }
  6. johhny
    Member
    Posted 1 year ago #

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

  7. johhny
    Member
    Posted 1 year ago #

    opsss forgot to mark as resolve.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    You're welcome. I'm glad you got it resolved :-)

Topic Closed

This topic has been closed to new replies.

About this Topic