Support » Developing with WordPress » How to Exclude Child Category from Display in Parent ones on Frontpage

  • Resolved Parmen

    (@parmen)


    As you know, for some reason WP employs the all-inclusive strategy when listing posts in parent categories. What we all need is a

      control

    to include or exclude child category posts from being displayed in the parent category.

    What I found as a solution to work with all archives and admin blocks on pages is this snippet to be added in category.php

    /**
     * Excludes child posts from parent category.
     *
     */
    if ( !function_exists('fb_filter_child_cats') ) {
    	function fb_filter_child_cats( $cats ) {
    		global $wp_query, $wpdb;
    		if ( is_category() ) {
    			// get children ID's
    			if ( $excludes = get_categories( "child_of=" . $wp_query->get('cat') ) ) {
    				// set array with ID's
    				foreach ( $excludes as $key => $value ) {
    					$exclude[] = $value->cat_ID;
    				}
    			}
    			// remove child cats
    			if ( isset($exclude) && is_array($exclude) ) {
    				$cats .= " AND " . $wpdb->prefix . "term_relationships.term_taxonomy_id NOT IN (" . implode(",", $exclude) . ") ";
    			}
    		}
    		return $cats;
    	}
    	if ( !is_admin() ) {
    		add_filter( 'posts_where', 'fb_filter_child_cats' );
    	}
    }

    This works just fine with the latest WP and themes for all instances – except for the front page which is a multi-block /home page designed with Blogshere. When in the admin creating the page, blocks are displaying correctly ONLY the parent category posts, BUT when published as a /home page OR assigned as frontpage, posts are displayed from the parent and all child categories.

    Interestingly, when opening a parent category page, it contains posts from the parent category only… Where could be the problem?

    TiA

    • This topic was modified 3 months, 2 weeks ago by Parmen.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • “is_category()” returns true if it’s a category archive page so it only works on the category page. You’d need a way to filter on the front page. You could create a custom template with your exact conditions and display it as a custom block or shortcode.

    If instead, you want to modify the above snippet replacing “is_category()” with something like “! empty( get_the_category() )” should work. (Untested)

    • This reply was modified 3 months, 2 weeks ago by Nigel M Rodgers. Reason: changed "get_the_category()" to "! empty( get_the_category() )
    Thread Starter Parmen

    (@parmen)

    Thank you for the input, Nigel.

    “get_the_category()” reverted the parent category page to the old state of all-inclusiveness.

    PS. The same with the revision.

    Will try with a home template, or using an empty category page as a placeholder for the home page blocks…

    • This reply was modified 3 months, 2 weeks ago by Parmen.
    • This reply was modified 3 months, 2 weeks ago by Parmen.

    Update. I’ve just checked and the default “Latest Posts” block displays only parent category posts on the front page. Please verify which block you’re using to list posts in a category.

    Thread Starter Parmen

    (@parmen)

    “Latest posts block lite” of AFThemes

    Probably the easiest way is to add some css instruction in the Additional CSS classes section of any block. At least this may solve the other problem with the long titles.

    Thank you for the lead.

    • This reply was modified 3 months, 2 weeks ago by Parmen.
    Thread Starter Parmen

    (@parmen)

    Problem solved. Found a plugin that does the trick and has a special control for excluding child categories, see Category Posts Widget

    wordpress.org/plugins/category-posts/

    And it does also the line truncation of both titles and excerpts.

    • This reply was modified 3 months, 2 weeks ago by Parmen.
    • This reply was modified 3 months, 2 weeks ago by Parmen.

    Awesome! I’m relieved there’s an alternative plugin.

    I dug a little into getting a hook that works on the front page and discovered that it’s not as simple as pasting it in functions.php as there may be some javascript involved which is beyond my current skill level.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.