WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How do I exclude category from blog page (21 posts)

  1. henrylemmon
    Member
    Posted 1 year ago #

    I have tried all the code in the codex like:

    `<?php
    if ( is_home()) {
    query_posts($query_string . '&cat=-17' );
    }
    ?>

    it will exclude category, but seems to be resetting query also.

    please help

  2. Hristo SiteGround
    Member
    Posted 1 year ago #

    Hello,

    Yes, it will reset the query. You need to add it before the loop in order to make it work correctly. In addition, you may need to use the wp_reset_query(); function to reset the query after filtering the category in order to keep your other functionality working.

    Please paste your entire code and tell me more about the final result you're after and I will happily further assist you :)

  3. henrylemmon
    Member
    Posted 1 year ago #

    Here is all my code

    http://pastebin.ca/2251866

    the suspect code is at the top of the loop.

    the code does exclude the category, and it displays the 3 newest posts, but when you use the nav arrows to go to the older posts it appears to cycle through the posts, displaying the same three newest posts until it finally runs through the posts and lands on a 404

    I am trying to exclude a category from the blog page so that feature it in a post gallery with a custom category template. it all works, but I can't get the category excluded from the blog page without buggering the blog display and navigation.

    thanks
    henrylemmon>

  4. henrylemmon
    Member
    Posted 1 year ago #

    The site is at:

    http://henrylemmon.com

    All the category's are listed on the blog page and the category I am trying to exclude is the gears.

    The theme is a child theme I created based on the twentytem theme

    This site is just a design I am attempting to create for learning purposes. thanks for your help.

  5. Hristo SiteGround
    Member
    Posted 1 year ago #

    Hello,

    Try adding <?php wp_reset_query(); ?> on line 46 of your code. That should do the trick.

  6. henrylemmon
    Member
    Posted 1 year ago #

    Ok I made the change and it just ignores the custom query all together now, and runs through all the posts in reverse chronological order.
    http://pastebin.ca/2252196

    henry

  7. Hristo SiteGround
    Member
    Posted 1 year ago #

    Hi Henry,

    I have done few tests and it turned out that this is quite some issue. However, I have found a workaround. Remove the line I've previously asked you to add and try installing the WP-PageNavi plugin. Then replace the standard pagination code with the one, provided by the plugin and it should work just find. You can find it in the WordPress Plugin Directory.

  8. henrylemmon
    Member
    Posted 1 year ago #

    Ok Hristo,
    I will use a plugin. Thank you for following up.
    henrylemmon>

  9. Hristo SiteGround
    Member
    Posted 1 year ago #

    Hello,

    I'd like to follow up this post. There is another solution :) Simply add:

    'paged'=> $paged

    to your query and the pagination will work just fine :)

  10. henrylemmon
    Member
    Posted 1 year ago #

    How would that code block look with taht addition of:

    'paged'=> $paged

    placed into:

    <?php
     if ( is_home()) {
     query_posts($query_string . '&cat=-17' );
     }
     ?>

    thank you
    henrylemmon>

  11. Hristo SiteGround
    Member
    Posted 1 year ago #

    It should go like this:

    <?php if ( is_home()) {
     query_posts($query_string . '&cat=-17&paged='.$paged );
     }?>
  12. henrylemmon
    Member
    Posted 1 year ago #

    I can exclude the category from the front page successfuly with the code:

    <?php
    if ( is_home()) {
     query_posts($query_string . '&cat=-6' );
     }
     ?>

    and then when we navigate to the excluded category I can view all of the posts successfuly, great!

    Question:
    Now when I navigate to the excluded category I would like to be abled to view only 3 posta at a time (but only in that category), and be allowed to navigate through the category if there is more than 3 posts.

    So now basically I got that cat off the home page and it has 10-15 posts in it and I only want to see three at a time and be able to navigate through the other posts in that cat.

    thank you
    henrylemmon>

  13. henrylemmon
    Member
    Posted 1 year ago #

    I found out what the problem is, you the above code works on a page but will not work on an archive.

  14. Hristo SiteGround
    Member
    Posted 1 year ago #

    Hello,

    I've been using this successfully on all types of pages. Check out whether you aren't reseting the query or there is something else you 're doing in particular in your archive.php file that causes the issue.

  15. henrylemmon
    Member
    Posted 1 year ago #

    Here is the whole story:

    1. built a child theme
    2. populated it with posts
    3. attempted to exclude a category from the main blog page using the code below, so I could use a custom category page for that category only
    4. if ( is_home() || is_archive()) {
      	query_posts($query_string . '&cat=-17&posts_per_page=3' );
      	}

      if I use this code, cat 17 is removed from main blog page and archive and you can only see 3 of the latest posts in chronological order. even if you do a search, click on the flowers category you still get the same three posts which are the 3 most recent posts regardless of the search criteria or what you have clicked on.
      so if I remove:
      &posts_per_page=3
      you get all the posts listed in reverse chrono excluding the cat 17 even if you click on flower in the menu or the archives you canot get to flowers
      so when I remove
      || is_archive()
      and now it reads

      if ( is_home()) {
      	query_posts($query_string . '&cat=-17' );
      	}

      category 17 will be removed from the main blog page, and everything works correctly when I search etc but flowers show up in the archives.

      I think that I understand what is going on. You can't remove a category from the archives, because the archives does not list stuff by category, so unless we associate those specific posts with a group of some sory within archives it may never work
      ????????????????
      henrylemmon>

  16. Morten Rand-Hendriksen
    Member
    Posted 1 year ago #

    The query_posts() method for excluding categories in an index is an old method. There is a new and better one using pre_get_posts() and is_main_query() in a function placed in functions.php in your theme or child theme. There is a detailed explanation in the Codex but for simplicity here is the code example altered to match your requirements:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-17' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    WordPress is constantly evolving and the pre_get_posts() and is_main_query() functions are fairly new which is why they are not often featured in tutorials. The concept of using a function to alter the main query rather than doing it in a loop or template file is also fairly new.

    Try it out and report back.

  17. casper14209
    Member
    Posted 9 months ago #

    This code worked perfect for me with one change:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-17' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    the '-' is not needed to remove the category, when the '-' was present it only displayed that category.

    Here is the exact code used:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '3' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    I'm guessing that the 'exclude_category' and the '-' were two opposites which caused it to filter all except cat 3.

  18. Frank Gomez
    Member
    Posted 8 months ago #

    if I'm trying to apply the same code to a specific page how would I specify the page by name or ID instead of home.

  19. Frank Gomez
    Member
    Posted 8 months ago #

    Sorry I take that back.
    I want to exclude categories after I click on a post on the blog page. So the post navigation (previous / next) needs to exclude posts of a specific category. BUT on another page the post navigation (previous / next) needs to exclude ALL other categories except the one I want (which was excluded from the blog page).

  20. casper14209
    Member
    Posted 8 months ago #

    @Frank
    This is actually unrelated to this thread and there is another thread that already exists.
    Can't exclude a category from the previous_post_link and next_post_link
    also reference:
    Function Reference/previous post link
    Function Reference/next post link

  21. Frank Gomez
    Member
    Posted 8 months ago #

    Thanks I found those but they don't work, I'm sure it is a theme issue. I'm working on it from another angle.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.