• Resolved henrylemmon

    (@henrylemmon)


    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

Viewing 15 replies - 1 through 15 (of 20 total)
  • Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    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 ??

    Thread Starter henrylemmon

    (@henrylemmon)

    Here is all my code

    https://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>

    Thread Starter henrylemmon

    (@henrylemmon)

    The site is at:

    https://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.

    Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    Hello,

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

    Thread Starter henrylemmon

    (@henrylemmon)

    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.
    https://pastebin.ca/2252196

    henry

    Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    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.

    Thread Starter henrylemmon

    (@henrylemmon)

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

    Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    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 ??

    Thread Starter henrylemmon

    (@henrylemmon)

    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>

    Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    It should go like this:

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

    (@henrylemmon)

    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>

    Thread Starter henrylemmon

    (@henrylemmon)

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

    Hristo Pandjarov

    (@hristo-sg)

    SiteGround Representative

    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.

    Thread Starter henrylemmon

    (@henrylemmon)

    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>

    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.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘How do I exclude category from blog page’ is closed to new replies.