• Resolved AlternativePhotography

    (@alternativephotography)


    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!

Viewing 15 replies - 1 through 15 (of 20 total)
  • 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.

    Thread Starter AlternativePhotography

    (@alternativephotography)

    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?

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

    Thread Starter AlternativePhotography

    (@alternativephotography)

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

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

    Thread Starter AlternativePhotography

    (@alternativephotography)

    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?

    Thread Starter AlternativePhotography

    (@alternativephotography)

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

    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…

    Thread Starter AlternativePhotography

    (@alternativephotography)

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

    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' );

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

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

    Thread Starter AlternativePhotography

    (@alternativephotography)

    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?

    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: https://css-tricks.com/almanac/selectors/f/first-of-type/

    Thread Starter AlternativePhotography

    (@alternativephotography)

    Hm… that is not working…

    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;
    }
Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘How to exclude categories from home?’ is closed to new replies.