WordPress.org

Ready to get started?Download WordPress

Forums

Query posts cat=-x in category.php - works? (7 posts)

  1. newboxters
    Member
    Posted 6 years ago #

    Hi

    <?php query_posts('cat=-3,-7'); ?>

    this code works in category.php?

    Because when I insert this code and enter in http://www.mysite.com/category/internet
    Doesn't just list the internet posts and except the posts of the category ID 3 and 7, but, lists the last posts, independent of the category.

    Did I find that very strange, is it lacking something in my code?

    Thank you.
    ----

    Full Code:

    <?php query_posts('cat=-3,-7'); ?>
    <?php if (have_posts()) : ?>
    
    	<?php while (have_posts()) : the_post(); ?>
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="Article: <?php the_title(); ?>"><?php the_title(); ?></a><br />
    		<?php the_excerpt(); ?>
    	<?php endwhile; ?>
    
    <?php else : ?>
    	'0' posts in the category</h2>
    <?php endif; ?>
  2. moshu
    Member
    Posted 6 years ago #

    I don't understand what are you trying to do.

    category.php is used by WP to display a list of posts in ONE category - when you click on that category's name (e.g. in the sidebar). For that normal, general usage of the template file you do NOT need any trickery with the query_posts.

    So, now instead of posting code that that is not readable anyway... explain WHAT do you want to achieve and in which view?

  3. newboxters
    Member
    Posted 6 years ago #

    Ok. My problem is that the menu (sidebar) of the blog I have left to list only categories parents, and with that, when I click on one, opens the visual file category.php listing the latest posts of the category father and their subcategories. And in this case, in one of them, also displays the subcategories of id 3 and 7, and I wanted to exclude them from the listing.

    Thus the category father of id = 2 which list also subcategories of id 3 to 12, and I want NOT list the posts of the subcat id 3 and 7 which are not relevant because appear at the end of the site (pinneds).

    I hope that now I have explained better what I want to do.

    Thanks.

  4. moshu
    Member
    Posted 6 years ago #

    I can see now... but I don't think you can use the "minus exclude" twice. Al the examples I have ever seen were using it to exclude one category only - like: -3.

  5. newboxters
    Member
    Posted 6 years ago #

    In the page of documentation of the wordpress on query posts:
    http://codex.wordpress.org/Template_Tags/query_posts

    You can also add some more categories to the exclude-list(tested with WP 2.1.2):

    <?php
    if (is_home()) {
    query_posts("cat=-1,-2,-3");
    }
    ?>

    Had that code tested with the version 2.1.2 and as I am using the last 2.3.1 I thought was compatible.

    But when I place an only category him until it excludes in the exhibition (e.g. -3), but stops filtering for the posts of the category and it starts to exhibit all the others.

    Some suggestion to get that what is trying to do?

  6. moshu
    Member
    Posted 6 years ago #

    Well, that example you quoted is under this title:
    3.1 Exclude Categories From Your Home Page

    I tried your code on a test installation - it doesn't work.
    As a workaround I would suggest you remove cat 3 and 7 from under the cat 2 as children.

  7. newboxters
    Member
    Posted 6 years ago #

    Thank you for your suggestion, but it doesn't still work in my case, because the one that I did went to create a special "category" and when I will publish some very good content, I select your category and also the special (that is listed as main prominence in the home of the site). And that same special list (that exhibits last 3 special posts) they appear in the end of each category.

    Therefore, I wanted to filter for not exhibiting the special category in category.php for the same post not to be duplicated in a same page.

    But finally, is there some better way of highlight special posts?

Topic Closed

This topic has been closed to new replies.

About this Topic