WordPress.org

Forums

Post Category Only
Show category on main (2 posts)

  1. nazenaze
    Member
    Posted 4 years ago #

    I've been wondering if there's any way to use this awesome plugin to show just posts from one category on my Home page.

    I've been using this workaround to show them, but it shows child categories too.

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('cat', '7');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    and here's the original 'Post category only' plugin:

    class post_category_only {
    	static function load() {
    		add_action('pre_get_posts', array('post_category_only', 'pre_get_posts'));
    	}
    	static function pre_get_posts($argument) {
    		if($argument->is_category) {
    			$category = get_category_by_path($argument->query['category_name']);
    			if($category) {
    				$argument->query_vars['category__in'] =  $category->cat_ID;
    			}
    		}
    	}
    }
    
    post_category_only::load();

    Thanks for help!

    http://wordpress.org/extend/plugins/post-category-only/

  2. naifamoodi
    Member
    Posted 4 years ago #

    The following code should do the trick:

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set('category__in', '7');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    As you can see, I have replaced 'cat' with 'category__in'. That will ensure posts from child categories aren't displayed.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Post Category Only
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic