Support » Fixing WordPress » Exclude a category from the loop

  • Sorry, this is a follow up to an earlier related post, but I want to exclude a category from the loop. I’ve seen related posts and know what I have to do (I just don’t know the exact syntax, but basically I know I need to add this:

    <?php query_posts(cat=’-4′); ?>

    to this:

    <?php
    		$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		query_posts("showposts=4&paged=$page");
    		?>
    
    		<?php while ( have_posts() ) : the_post(); ?>
    
    		<?php get_template_part('content','archive'); ?>
    
    		<?php endwhile; ?>

    …except I don’t know exactly, precisely, how to phrase the code. If you could show me exactly how to include it that would be great, but (even better) recommend a GREAT SOURCE for learning to write WP’s propietary .php code.

    Thank you,

    John

Viewing 15 replies - 1 through 15 (of 36 total)
  • You can add this in your functions.php file

    add_action('pre_get_posts', 'cat_to_exclude' );
    
    function 'cat_to_exclude( $wp_query ) {
    
        $excluding = array(4);  
    
               set_query_var('category__not_in', $excluding);
    
    }

    I havnt tested it but you can try this as well
    in your loop
    showposts=4&paged=$page&cid=-4

    add a minus(-) sign

    for further read you can follow this codex
    http://codex.wordpress.org/Custom_Queries#Category_Exclusion

    Thread Starter johndove523

    (@johndove523)

    Hmmm, I tried both methods, got a bad result. :

    unexpected T_CONSTANT_ENCAPSED_STRING…

    Thread Starter johndove523

    (@johndove523)

    I don’t know what I did, but I sure screwed up something. The whole site is down. It says the error is on line 108 in functions.php, which I never touched:

    function dynamictitles() {

    if ( is_single() ) { *********THIS IS LINE 108
    wp_title(”);
    echo (‘ | ‘);
    bloginfo(‘name’);

    ???

    How could you of tried both methods if you didn’t touch the functions.php file?

    If you have a copy of a clean functions.php file for your theme,(child ?) perhaps, now would be a good time to upload it to your theme folder.

    If you have made other changes to it, I hope you kept a hard copy to reupload.

    Thread Starter johndove523

    (@johndove523)

    No, I went into the function.php file and tried the method that cinghaman suggested above. It didn’t remove the category, so I went back to functions.php and deleted that code. That’s when everything went amuck. Strange thing is, I wasn’t anywhere near where the error is being reported (I added the code at line 78, didn’t touch anything near line 108). I have a clean local copy at work I’ll try tomorrow.

    just saw the code there was extra character in the function

    add_action('pre_get_posts', 'cat_to_exclude' );
    
    function cat_to_exclude( $wp_query ) {
    
        $excluding = array(4);  
    
               set_query_var('category__not_in', $excluding);
    
    }

    Create a child folder for your theme
    create a new functions file add the code

    Works fine for me tested on my own blog

    Thread Starter johndove523

    (@johndove523)

    Thanks cinghaman, got everything straightened out, and yes corrected code for the functions file works fine, but it eliminates the post on the home page too (category “home-page”, id=4). On the home page, the text under the photo is a post (id=4). I wanted to eliminate that post category from the latest “recent blog posts” in the upper left. I successfully did that, but then realized the post was still showing up on the blog (which I also don’t want). The functions.php method you suggested above works fine, but eliminates the post from the home page too.

    for that you can create a if statement and wrap the function inside

    if(!is_home()) {
    
    }

    Thread Starter johndove523

    (@johndove523)

    Thanks, but again, I don’t know the exact syntax… can you put it together for me?

    try this

    function cat_to_exclude($wp_query) {
      if ( !is_home() ) {
        $wp_query->set('category__not_in', '52');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'cat_to_exclude');

    Thread Starter johndove523

    (@johndove523)

    Nope, that completely wigged out my site again.

    52 has to be replaced with your 4 (that was i testing on my blog)

Viewing 15 replies - 1 through 15 (of 36 total)
  • The topic ‘Exclude a category from the loop’ is closed to new replies.