WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to exclude categories from home? (21 posts)

  1. AlternativePhotography
    Member
    Posted 10 months ago #

    I'm trying to exclude certain categories from home. Whatever i tried does not work, can anyone give me a hint please?
    Here is the code:

    <?php
    $posts = query_posts($query_string . '&posts_per_page=20');
    while ( have_posts() ) : the_post() ?>
    
    <div class="thumbnail">
    
    <h2 style="margin:0px; padding:0px">
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
    </h2>
    
    <div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    
    <h3 class="entry-title" style="margin-top:0px; padding-top:0px">
    <div class="floatright" style="padding:0px">
    <?php the_post_thumbnail( 'thumbnail' ); ?>
    </div>
    
    <a href="<?php the_permalink() ?>" title="<?php printf( __('Permalink to %s', 'sandbox'), the_title_attribute('echo=0') ) ?>" rel="bookmark"><?php the_title() ?></a></h3>

    I've tried this (does not work):

    $cats_to_exclude = array(

    69, // Call for entries

    16, // Competitions

    70, // Meetings & talks

    71, // Exhibitions

    72, // Workshops

    );

    Any better ideas? Very grateful!

  2. Shaun Scovil
    Member
    Posted 10 months ago #

    Never use query_posts(). Try adding this to your functions.php instead:

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

    Replace those category IDs with the ones you want to exclude. Note the negative sign before the ID, which tells it to exclude that category.

    See pre_get_posts() for more info.

  3. AlternativePhotography
    Member
    Posted 10 months ago #

    Hi Shaun!
    Thank you! Do I just add it anywhere on the functions.php page?
    I've added this, but it does not make any difference for some reason:

    // Function to exclude events categories from home page
    
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-16,-69,-70,-71,-72' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    
    // Function to exclude events categories ends

    Did i do anything wrong?

  4. Shaun Scovil
    Member
    Posted 10 months ago #

    You'll want to make sure this is at the beginning of your functions.php file, just after the opening <?php tag.

    If that is the case and it's still not working, I will need a little more information. Specifically, what theme are you using and what is the link to your website (so I can view it in a browser)?

  5. AlternativePhotography
    Member
    Posted 10 months ago #

    Thanks for helping me!
    I've got it right at the top. I've copied the code so you can see.
    Still not working. Using Sandbox, with a modified stylesheet and the website is http://www.AlternativePhotography.com

    <?php
    /*
    This file is part of SANDBOX.
    
    SANDBOX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
    
    SANDBOX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License along with SANDBOX. If not, see http://www.gnu.org/licenses/.
    */
    
    // Function to exclude events categories from home page
    
    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-16,-69,-70,-71,-72' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    
    // Function to exclude events categories ends
    
    // Produces a list of pages in the header without whitespace
    function sandbox_globalnav() {
    	if ( $menu = str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages('title_li=&sort_column=menu_order&echo=0') ) )
    		$menu = '<ul>' . $menu . '</ul>';
    	$menu = '<div id="menu">' . $menu . "</div>\n";
    	echo apply_filters( 'globalnav_menu', $menu ); // Filter to override default globalnav: globalnav_menu
    }
  6. alchymyth
    The Sweeper & Moderator
    Posted 10 months ago #

    'pre_get_posts' assumes that you are using a normal query, i.e. no query_posts() code;

    make sure to remove this line:

    $posts = query_posts($query_string . '&posts_per_page=20');

    what is the code of index.php or home.php of your theme?

  7. AlternativePhotography
    Member
    Posted 10 months ago #

    Here is the code from home.php:

    <?php get_header() ?>
    
    	<div id="container">
    		<div id="content">
    
    			<div id="nav-above" class="navigation">
    				<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'sandbox' )) ?></div>
    				<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'sandbox' )) ?></div>
    			</div>
    
    <h1 style="margin:0px; padding:0px">Love alternative processes?</h1>
    <p style="margin-top:0px; padding-top:0px">You've come to the right place. Enjoy the latest articles.</p>
    
    <?php
    $posts = query_posts($query_string . '&posts_per_page=20');
    while ( have_posts() ) : the_post() ?>
    
    <div class="thumbnail">
    
    <h2 style="margin:0px; padding:0px">
    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>
    </h2>
    
    <div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    
    <h3 class="entry-title" style="margin-top:0px; padding-top:0px">
    <div class="floatright" style="padding:0px">
    <?php the_post_thumbnail( 'thumbnail' ); ?>
    </div>
    
    <a href="<?php the_permalink() ?>" title="<?php printf( __('Permalink to %s', 'sandbox'), the_title_attribute('echo=0') ) ?>" rel="bookmark"><?php the_title() ?></a></h3>
    
    <?php
    $myExcerpt = get_the_excerpt();
    if ($myExcerpt != '') {
        // Some string manipulation performed
    }
    echo $myExcerpt; // Outputs the processed value to the page
    ?>
    
    			</div></div><!-- .post -->
    
    <?php comments_template() ?>
    
    <?php endwhile; ?>
    
    			<div id="nav-below" class="navigation">
    				<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'sandbox' )) ?></div>
    				<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'sandbox' )) ?></div>
    			</div>
    
    		</div><!-- #content -->
    	</div><!-- #container -->
    
    <?php get_sidebar() ?>
    <?php get_footer() ?>

    Shall i remove that line from the home.php?

  8. AlternativePhotography
    Member
    Posted 10 months ago #

    Removing that line from home.php still makes no difference. I've put it back again.

  9. Shaun Scovil
    Member
    Posted 10 months ago #

    The use of query_posts() has a lot of quirks and drawbacks. In this case, it is only being used to change the posts_per_page parameter to 20, which could easily be done in that function instead.

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-16,-69,-70,-71,-72' );
            $query->set( 'posts_per_page', '20' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );

    Then remove the query_posts() line from your page template.

    As far as why that is not working, let me look over the theme real quick...

  10. AlternativePhotography
    Member
    Posted 10 months ago #

    Ok, shall i remove just that line or all of this?

    <?php
    $posts = query_posts($query_string . '&posts_per_page=20');
    while ( have_posts() ) : the_post() ?>
  11. Shaun Scovil
    Member
    Posted 10 months ago #

    In WP-Admin > Settings > Reading, under Front page displays, are you set to display a static front page?

    If so, modify the function to this:

    function exclude_category( $query ) {
        if ( $query->is_front_page() && $query->is_main_query() ) {
            $query->set( 'cat', '-16,-69,-70,-71,-72' );
            $query->set( 'posts_per_page', '20' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
  12. Shaun Scovil
    Member
    Posted 10 months ago #

    No, should just end up with this in your index.php:

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

  13. AlternativePhotography
    Member
    Posted 10 months ago #

    Thank you! That works brilliant! Thank you for your help.
    One more question when i have a knowledgable person on the line... is there any way i can change the first post to have a different stylesheet? I would like the newest post to have a big splash, like in a newspaper... is this possible?

  14. Shaun Scovil
    Member
    Posted 10 months ago #

    Easiest way would be to single out the first post with CSS. In your case, you could do something like this:

    #content div.thumbnail:first-of-type {
        display: block;
        width: 840px;
    }

    That is untested; you may need to play around with it a bit. Here is more info about that CSS selector: http://css-tricks.com/almanac/selectors/f/first-of-type/

  15. AlternativePhotography
    Member
    Posted 10 months ago #

    Hm... that is not working...

  16. Shaun Scovil
    Member
    Posted 10 months ago #

    Right, that's because .thumbnail is a div type of element, and there is one div (.nav-above) before the thumbnails start.

    Try this:

    #content div.thumbnail:nth-of-type(2) {
        display: block;
        width: 840px;
    }
  17. AlternativePhotography
    Member
    Posted 10 months ago #

    That seems to work! Wow, you're so good at this.
    It works on all pages, is there any way I can make it just work on the homepage and not the other ones?

  18. Shaun Scovil
    Member
    Posted 10 months ago #

    Looks like your homepage has a body class of home, which I assume other pages do not have. If that is correct, try this:

    .home #content div.thumbnail:nth-of-type(2)

    But this is getting way off track from the original question; going forward you should really open a new thread for an unrelated question.

  19. AlternativePhotography
    Member
    Posted 10 months ago #

    You're right. Thank you. Opening a new thread. The stylesheet worked in the browser, but not when i do it for real.
    Bye and thanks.

  20. AlternativePhotography
    Member
    Posted 9 months ago #

    Hi Shaun,
    Here is the thread with the stylesheet issue:
    http://wordpress.org/support/topic/styling-the-first-post-differently-from-the-rest
    Someone is suggesting another solution, but i can't get it to work. I think you were close with the css idea... it worked when i put it in the change css in firefox, but not when pasting it into the real stylesheet... any thoughts? (on the new thread)

  21. AlternativePhotography
    Member
    Posted 9 months ago #

    Just marking the initial thread as resolved.

Reply

You must log in to post.

About this Topic