1

I am using the eList wordpress theme.

Now on the main page, there is a thumbnail display of all the categories listed. Now, on the theme you will see that they have no sub-categories under the categories, only listings

Now in my theme, I have sub-categories, and the theme does make provision for that. See my site here and the dropdown list next to the search bar in the header to see what I mean about the subcategories

Question:

How do I only list the MAIN categories on the home page, and NOT the sub-categories of the main categories as well?

Here is the code of the home page part that controls the thumbnails:

<?php
    $elist_categories_args = array( 'hide_empty' => 0 );

    if ( 'on' == get_option('elist_listings_hide_empty') ) $elist_categories_args['hide_empty'] = 1;

    if ( is_tax() ) {
        $et_term = get_queried_object();
        $elist_categories_args['child_of'] = $et_term->term_id;
    }

    $categories = get_categories( 'taxonomy=listing-category' );

    $elist_listing_categories = get_terms( 'listing_category', apply_filters( 'listing_categories_args', $elist_categories_args ) );
    $elist_category_images = false !== get_option( 'elist_category_images' ) ? (array) get_option( 'elist_category_images' ) : array();
    $et_count = 0;

    if ( $elist_listing_categories ){ ?>
        <section id="listing-categories">
            <div class="container clearfix">
                <h1><?php esc_html_e( 'Listing Categories', 'eList' ); ?></h1>
                <?php foreach( $elist_listing_categories as $elist_listing_category ) { ?>
                    <?php
                        $et_current_term_query = new WP_Query( 
                            array(
                                'post_status' => 'publish',
                                'tax_query' => array(
                                        array(
                                            'taxonomy' => 'listing_category',
                                            'field' => 'id',
                                            'terms' => $elist_listing_category->term_id
                                        )
                                    )
                            )
                        );
                    ?>

                    <?php $et_count++; ?>
                    <div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
                        <?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
                        <?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
                            <div class="thumb">
                                <a href="<?php echo esc_url( $et_listing_category_link ); ?>">
                                    <img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
                                    <span class="overlay"></span>
                                </a>
                            </div>  <!-- end .thumb -->
                        <?php } ?>
                        <div class="description">
                            <h2><a href="<?php echo esc_url( $et_listing_category_link ); ?>"><?php echo esc_html( $elist_listing_category->name ); ?></a></h2>
                            <p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
                            <?php if ( '' != $elist_listing_category->description ) { ?>
                                <p><?php echo esc_html( $elist_listing_category->description ); ?></p>
                            <?php } ?>
                        </div> <!-- end .description -->    
                    </div> <!-- end .l-category -->
                <?php } ?>
            </div> <!-- end .container -->
        </section> <!-- end #listing-categories -->
<?php } ?>

To pinpoint out of the above code, here is the html part that holds the dynamically populated thumbnail:

<div class="l-category<?php if ( $et_count % 3 == 0 ) echo ' last'; ?>">
                        <?php $et_listing_category_link = get_term_link( $elist_listing_category ); ?>
                        <?php if ( isset( $elist_category_images[$elist_listing_category->term_id] ) && '' != $elist_category_images[$elist_listing_category->term_id] ) { ?>
                            <div class="thumb">
                                <a href="<?php echo esc_url( $et_listing_category_link ); ?>">
                                    <img class="item-image" alt="<?php echo esc_attr( $elist_listing_category->name ); ?>" src="<?php echo esc_attr( et_new_thumb_resize( et_multisite_thumbnail( $elist_category_images[$elist_listing_category->term_id] ), 70, 70, '', true ) ); ?>"/>
                                    <span class="overlay"></span>
                                </a>
                            </div>  <!-- end .thumb -->
                        <?php } ?>
                        <div class="description">
                            <h2><a href="<?php echo esc_url( $et_listing_category_link ); ?>"><?php echo esc_html( $elist_listing_category->name ); ?></a></h2>
                            <p class="info"><?php if ( 1 == $et_current_term_query->found_posts ) printf( __('%d Listing','eList'), $et_current_term_query->found_posts ); else printf( __('%d Listings'), $et_current_term_query->found_posts ); ?></p>
                            <?php if ( '' != $elist_listing_category->description ) { ?>
                                <p><?php echo esc_html( $elist_listing_category->description ); ?></p>
                            <?php } ?>
                        </div> <!-- end .description -->    
                    </div> <!-- end .l-category -->

2 Answers 2

1

Ideally, place this in a plugin (so it's not overwritten when you update the theme):

function wpse_106185_elist_categories( $args ) {
    return $args + array(
        'depth' => 1,
    );
}

add_filter( 'listing_categories_args', 'wpse_106185_elist_categories' );

Alternatively, a quick fix is to edit this line...

$elist_categories_args = array( 'hide_empty' => 0 );

To...

$elist_categories_args = array( 'hide_empty' => 0, 'depth => 1 );
4
  • Thank you TheDeadMagic, but I tried the quickfix, and it does not work, no change Commented Jul 12, 2013 at 13:09
  • Try a depth of 0 Commented Jul 13, 2013 at 2:04
  • still does not work. But thanks hey, have given up, tried a different solution. Commented Jul 19, 2013 at 12:17
  • @DextrousDave Post it as a solution! Commented Jul 26, 2013 at 11:29
1

Paste this at the end of your child themes functions.php file

function fb_filter_child_cats($query) {

$cat = get_term_by('name', $query->query_vars['category_name'], 'category');
$child_cats = (array) get_term_children( &$cat->term_id, 'category' );
// also possible
// $child_cats = (array) get_term_children( get_cat_id($query->query_vars['category_name']), 'category' );

if ( !$query->is_admin )
    $query->set( 'category__not_in', array_merge($child_cats) );

return $query;
}
add_filter( 'pre_get_posts', 'fb_filter_child_cats' );

Source http://wpengineer.com/876/exclude-subcategories-in-a-loop/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.