ThemeShaper Forums » Thematic

[closed]

Exclude a category from index loop

(11 posts)
  • Started 2 years ago by alexgagi
  • Latest reply from bruinmark
  • This topic is resolved
  1. Hi all. My page is http://jazztk.com and I want to exclude one category from the index loop. What will be the best way to do this? Replace the loop? Or I can use the above_index_loop? (in the las case, how I can do this?)
    My knowledge about php is quite small, sorry.

    Thanks in advance.

    Álex.

    Posted 2 years ago #
  2. Sorry, I may be a little confused by the question. If you are looking to exclude a category from your homepage. Try..


    //exclude post categories from home if default is blog
    //http://forums.themeshaper.com/topic/exclude-posts-from-specific-categories
    function excludeposts() {
    if (is_home()) {
    query_posts("cat=-25, -26");
    }
    }
    add_filter ('thematic_above_indexloop','excludeposts');

    The commented out link will also have more info for you to check into if you have trouble, you will need to set category ID's and possibly change the conditional part (is_home()) depending on your setup.

    Posted 2 years ago #
  3. Terrific. You understood perfectly. That was exactly what I needed and has worked so well.

    Thanks a lot.

    Posted 2 years ago #
  4. Sorry, I was too fast... Well, it works nice on the homepage, but when I go to the second page, I see the same posts of the first page, not the older ones.

    Any ideas?

    Posted 2 years ago #
  5. i'm sort of surprised add_filter worked there, but i guess there isn't anything else on that hook. i'd have used add_action

    but to fix your problem i think you need to merge the current query args w/ the ones you want to change. specifically you need to maintain the $paged variable or you end up w/ what you described.

    http://codex.wordpress.org/Function_Reference/wp_parse_args

    give this a whirl:

    function excludeposts() {
    global $wp_query;
    
    	if (is_home()) {
    		$defaults = $wp_query->query_vars;
    		$exclude = array('category__not_in'=> array(25,26));
    		$args = wp_parse_args( $exclude, $defaults );
    		query_posts($args);
    	}
    }
    add_action('thematic_above_indexloop','excludeposts');
    Posted 2 years ago #
  6. Thank you, helga. Perfect solution. There's so much I need to learn about wp and php...

    Álex.

    Posted 2 years ago #
  7. bruinmark
    Member

    I'm a noob with Thematic and to a lesser extent WP (both are great!). Question for helga or anyone: I tested helga's solution above on a child theme I'm doing. It works, but: the order of posts seems to be wrong (it's either ASC or mixed up, I can't tell which). But it's definitely not the default DESC with newest results on top.

    Is there an easy solution for this? Do I have to do a new query_posts() statement before the Loop? Am I doing something wrong?

    Any help from these awesome forums appreciated!

    Posted 2 years ago #
  8. this is no longer the way i recommend excluding a category from the index loop as it requires 2 queries. the more efficient way is to modify the query before it is ever run using the pre_get_posts() action

    http://codex.wordpress.org/Custom_Queries#Category_Exclusion

    add_action('pre_get_posts', 'kia_exclude_cat' );
    
    function kia_exclude_cat( &$wp_query ) {
    
    	// Figure out if we need to exclude glossary - exclude from
    	// archives (except category archives), feeds, and home page
    	if( is_home() || is_feed() || ( is_archive() && !is_category() )) {
    		set_query_var('category__not_in', array(25,26));
    	}
    }
    Posted 2 years ago #
  9. bruinmark
    Member

    Thanks, that definitely works (I simplified it because I'm just excluding one category from home page)...

    add_action('pre_get_posts', 'exclude_category_five' );
    
    function exclude_category_five( &$wp_query ) {
    
    	// Exclude category 5 from home
    	if ( is_home() ) {
    		set_query_var('category__not_in', 5);
    	}
    }

    But: same problem happens. Posts in category 5 are excluded as expected, but the remaining posts do not appear in DESC order. (Can't show you a live link... install is on localhost.) I'm sure I'm missing something or doing something wrong. Any other thoughts?

    Posted 2 years ago #
  10. you must have done something wrong somewhere else, or there is a plugin causing problems, b/c there is nothing in that code snippet that would reverse the order.

    http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    desc order is the default query.

    if you are sure they are being reversed, remove everything else from your functions, check you aren't using some kind of template, and disable all plugins.

    Posted 2 years ago #
  11. bruinmark
    Member

    Thanks for your input, helga. Great suggestion. I went back to functions.php for my child theme and removed all the code I had added. Result: Query is in DESC order as it should be. Then I added in only the exclude_category_five function I mentioned above, and it works as planned (omitting category id #5), but posts are definitely no longer in DESC order. So conflict with other functions isn't the cause. (As an aside, all posts are in the correct DESC order when I check each /category.)

    Beyond that, there are no other page templates or additional PHP that I know of in my WP install. It's just Thematic and my Thematic child theme.

    I only have five active plugins (Contact Form 7, Contact Form to DB Extension, Really Simple CAPTCHA, Widget Logic and WP Google Fonts), and I'm doubting they're the source of this.

    Kind of interesting. Anyway, will explore this further and report back if/when I find the cause.

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.