WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] exclude posts of specific category (11 posts)

  1. daitya
    Member
    Posted 3 years ago #

    Using Theme: Twentyten (child)
    Installation: local, multisite network enabled

    Aim:
    To exclude posts from category id 9 in the main blog section of the home page (because those posts are already called into a separate text area of the same page, so in the blog section I don’t want them duplicated).

    I’ve tried the following:

    <?php /* For home page only */ ?>
    <?php if (is_home() : ?>
    <?php query_posts(’cat=-9′); ?>

    placed this inside loop-index.php immediately after <?php /* How to display all other posts. */ ?> (inside the loop). As a result, all blog posts disappear from my home page (except those in the separate text area).

    Help?

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Don't edit the Twenty Ten theme! Your changes will be over-written the next time you upgrade WordPress or the theme. For this reason, it is recommended that you consider creating a child theme for your customisations.

  3. vtxyzzy
    Member
    Posted 3 years ago #

    Using Theme: Twentyten (child)

    In your child theme loop-index.php, try changing this:

    <?php /* How to display all other posts. */ ?>
    
    	<?php else : ?>
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    to this:

    <?php /* How to display all other posts. */ ?>
    
    	<?php else : ?>
    		<?php if (is_front_page())
    			query_posts(array_merge($wp_query->query,
    				array('category__not_in' => 9)));
    		?>
    
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  4. daitya
    Member
    Posted 3 years ago #

    Thanks, Esmi - I had created index-loop.php inside the child theme (there is no index-loop.index in Twentyten).

    And thank you, vtxyzzy for your effort to reply, but that really threw my home page for a loop! I ended up with only that post from that category, repeated I don't know how many times - I stopped counting after 13, and the scroll bar was way up at the top of the browser. :o

    Surely there are many wordpress users who have need for excluding posts from certain categories and who have figured out some way to do it, but my keyword searches in Google and WP so far have not turned up a solution that works here.

    Esmi, have you not come across this kind of thing before?

  5. esmi
    Forum Moderator
    Posted 3 years ago #

    I had created index-loop.php inside the child theme

    That file should be called loop-index.php.

  6. vtxyzzy
    Member
    Posted 3 years ago #

    Very sorry - I got in a hurry and totally blew it. The new query needs to go way up at the top of the file. Please try this instead:

    * @package WordPress
     * @subpackage Twenty_Ten
     * @since Twenty Ten 1.0
     */
    ?>
    
    <?php if (is_front_page())
    	query_posts(array_merge($wp_query->query,
    		array('category__not_in' => 9)));
    ?>
    
    <?php /* Display navigation to next/previous pages when applicable */ ?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
  7. daitya
    Member
    Posted 3 years ago #

    Hi Esmi, it is called loop-index.php (sorry, I was typing off the top of my head).

    Vtxyzzy, I just plugged it in, but it doesn't seem to do anything at all. I changed is_front_page() to is_home() and still no effect.

    I emptied FF cache. Refreshed. No change. Quit FF, quit Apache and MySQL servers. Restarted servers, opened start page in FF. Still no change. The post from the supposedly excluded category is still duplicated in the blog section.

  8. daitya
    Member
    Posted 3 years ago #

    Finally stumbled upon http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category. Inserted this code in the file loop-index.php:

    <?php
    /* Start the Loop.
    *
    * In Twenty Ten we use the same loop in multiple contexts.
    * It is broken into three main parts: when we're displaying
    * posts that are in the gallery category, when we're displaying
    * posts in the asides category, and finally all other posts.
    *
    * Additionally, we sometimes check for whether we are on an
    * archive page, a search page, etc., allowing for small differences
    * in the loop on each template without actually duplicating
    * the rest of the loop that is shared.
    *
    * Without further ado, the loop:
    */ ?>
    <?php if ( is_home() ) {
    query_posts($query_string . '&cat=-9');
    }
    ?>

    <?php while ( have_posts() ) : the_post(); ?>

    Happy to report that has eliminated the duplication of the post from that category.

    However, debugging reports:

    Notice: Undefined variable: query_string in /Users/das/Documents/Websites/Bhaktivedantas-dev/public_html/wp-content/themes/twentyten-child/loop-index.php on line 58

    Should I pay any attention to this? Is it an error that is going to screw things up somewhere?

    Thanks again

  9. vtxyzzy
    Member
    Posted 3 years ago #

    Please be sure you used 2 underscores between category and not in the query:

    'category__not_in' => 9,  // like this
    'category_not_in' => 9,   // not like this
  10. daitya
    Member
    Posted 3 years ago #

    But vtxyzzy, I've used exactly this:

    <?php
    //to exclude posts from category id 9
    //and limit number of posts to 3
    if ( is_home() ) {
    query_posts($query_string . '&cat=-9&posts_per_page=3' );
    }
    ?>

    And so far it's working on the home page. I didn't use the array structure at all.

  11. vtxyzzy
    Member
    Posted 3 years ago #

    The array structure would work if entered properly.

Topic Closed

This topic has been closed to new replies.

About this Topic