• Hello everyone,

    i am new to this forum and i am not sure if my post is in the correct category, so please forgive me if i post it in the wrong place.

    I am using a custom made widget to display the categories list on my website and i try to exclude few specific categories but i can’t find the way to do it.

    I will post the code below and if anyone knows how i can modify it to exclude few specific categories, i will appreciated!

    class widget_category_icons extends WP_Widget {
    
    	function widget_category_icons() {
    	/*Constructor*/
    		$widget_ops = array( 'classname' => 'widget_category_icons ', 'description' => __( 'Category with representative icons' , 'cosmotheme' ) );
    		$this->WP_Widget( 'widget_cosmo_category_icons', _TN_ . ' : ' . __( 'Category icons' , 'cosmotheme' ) , $widget_ops );
    	}
    
    	function widget( $args, $instance ) {
            /* prints the widget*/
    
            if( isset( $instance[ 'title' ] ) ){
                $title = $instance[ 'title' ];
            }else{
                $title = '';
            }
    
            extract( $args , EXTR_SKIP );
    
            echo $before_widget;
    
            if( !empty( $title ) ){
                echo $before_title . $title . $after_title;
            }
    
            $categories = get_terms( 'category' );
    
            ?><ul><?php
            foreach( $categories as $category ){
    
                if( isset( $instance[ 'icon_' . $category -> term_id ] ) ){
                    $icon[ 'icon_' .  $category -> term_id ] = $instance[ 'icon_' .  $category -> term_id ];
                }else{
                    $icon[ 'icon_' .  $category -> term_id ] = 'default.png';
                }
                ?>
                    <li class="cat-item cat-item-3 cat-item-icon">
                        <a href="<?php echo get_category_link( $category ); ?>" title="<?php printf(__('View all posts filed under %s','cosmotheme'),$category -> name); ?>">
                            <?php
                                if( !empty( $icon[ 'icon_' .  $category -> term_id ]  ) ){
                            ?>
                            <img src="<?php echo get_template_directory_uri(); ?>/images/category_icons/<?php echo $icon[ 'icon_' .  $category -> term_id ]; ?>" alt="" />
                            <?php
                                }
                            ?>
                            <span><?php echo $category -> name ?></span>
                        </a>
                    </li>
                <?php
            }
            ?></ul>

    Thank you

Viewing 1 replies (of 1 total)
  • do you want to hard-code the to-be-excluded categories as an array of cat IDs?

    if so, try to add a line like this directly after the ‘foreach’ line;

    example:

    if( in_array( $category->term_id, array( 1, 7, 23 ) ) ) continue;

Viewing 1 replies (of 1 total)
  • The topic ‘Excluding Categories from Custom Widget’ is closed to new replies.