WordPress.org

Ready to get started?Download WordPress

Forums

Exclude a category from the loop (37 posts)

  1. johndove523
    Member
    Posted 1 year ago #

    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

  2. cinghaman
    Member
    Posted 1 year ago #

    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);
    
    }
  3. cinghaman
    Member
    Posted 1 year ago #

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

    add a minus(-) sign

  4. cinghaman
    Member
    Posted 1 year ago #

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

  5. johndove523
    Member
    Posted 1 year ago #

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

    unexpected T_CONSTANT_ENCAPSED_STRING...

  6. johndove523
    Member
    Posted 1 year ago #

    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');

    ???

  7. dragonsjaw
    Member
    Posted 1 year ago #

    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.

  8. johndove523
    Member
    Posted 1 year ago #

    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.

  9. cinghaman
    Member
    Posted 1 year ago #

    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

  10. cinghaman
    Member
    Posted 1 year ago #

    Works fine for me tested on my own blog

  11. johndove523
    Member
    Posted 1 year ago #

    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.

  12. cinghaman
    Member
    Posted 1 year ago #

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

    if(!is_home()) {
    
    }
  13. johndove523
    Member
    Posted 1 year ago #

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

  14. cinghaman
    Member
    Posted 1 year ago #

    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');
  15. johndove523
    Member
    Posted 1 year ago #

    Nope, that completely wigged out my site again.

  16. cinghaman
    Member
    Posted 1 year ago #

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

  17. johndove523
    Member
    Posted 1 year ago #

    cinghaman, no, doesn't remove it from blog, does remove it from home page (the inverse of what I want to do).

  18. cinghaman
    Member
    Posted 1 year ago #

    can you give your website link

  19. johndove523
    Member
    Posted 1 year ago #

  20. cinghaman
    Member
    Posted 1 year ago #

    1: There is a post under the photo (which is from category 4 and you want that post to keep displaying)
    2:On left side (you are using a widget(?) from which you want to remove that category(4)post)
    3:On blog page you want the category 4 to be removed

    yes?

  21. johndove523
    Member
    Posted 1 year ago #

    All yes, except not a widget on # 2.

    In custom-home-page.php, :

    <?php query_posts(array('showposts' => 2, 'order' => 'dec', 'cat' => -4 )); while (have_posts()) { the_post(); ?>

  22. cinghaman
    Member
    Posted 1 year ago #

    and how are you calling category-home-page on your homepage ?

  23. johndove523
    Member
    Posted 1 year ago #

    Set a static home page, pointing to the home page template.

  24. johndove523
    Member
    Posted 1 year ago #

    Sorry, I think you meant the "home page" post category in home-page-template.php:

    <?php query_posts('cat=4'); ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<div class="home-page-article">
    	<h2><?php // the_title(); ?></h2>
    	<?php the_content(); ?>
    	</div>
    	<?php endwhile; ?>
  25. cinghaman
    Member
    Posted 1 year ago #

    if ( !is_home() || !is_front_page() )

    Replace the if with the above if

  26. johndove523
    Member
    Posted 1 year ago #

    Sorry, I don't see an "if"... what "if"?

  27. cinghaman
    Member
    Posted 1 year ago #

    function cat_to_exclude($wp_query) {
      if ( !is_home()|| !is_front_page() ) {
        $wp_query->set('category__not_in', '52');
      }
      return $query;
    }
    add_filter('pre_get_posts', 'cat_to_exclude');
  28. johndove523
    Member
    Posted 1 year ago #

    Removed correct category post from blog, but resulted in fatal error on home page.

  29. johndove523
    Member
    Posted 1 year ago #

    I changed 52 to 4.

  30. cinghaman
    Member
    Posted 1 year ago #

    what does the error say

Topic Closed

This topic has been closed to new replies.

About this Topic