• Resolved keithwjones

    (@keithwjones)


    This code is to exclude categories from Home page. It adds a section to Read Settings where you can add the categories to be excluded. I am told that it worked in WP 4.9.

    I can not get it to work in WP 5.2.1 PHP 7.3.3

    It adds an entry on Read Settings page, but just text which you can not add categories to. Anyone see a problem?

    
    /* CLASS CATEGORY EXCLUDE FROM HOME and paged pages
    excludes selected categories from main loop on the blog home page
    controlled by text input under dashboard->settings->reading -> 'Exclude Categories from Home' */
    
    class custom_exclude_categories {
    public function custom_exclude_categories() {
    $this->__construct();
    }
    public function __construct() {
    add_action('admin_init',array($this,'register_setting'),10,0);
    add_action('admin_menu',array($this,'admin_option_setup'),10,0);
    add_action('pre_get_posts',array($this,'pre_get_posts'),20,1);
    }
    public function register_setting() {
    register_setting('reading','excluded_categories');
    }
    public function admin_option_setup() {
    add_settings_field( 'excluded_categories_field', __('Exclude Categories from Home', 'custom_lang'), array($this,'excluded_categories_field'), 'reading', 'default' );
    }
    public function excluded_categories_field() {
    $value = get_option('excluded_categories');
    echo '
    
    ';
    echo '
    '. __('Enter here comma separated IDs of the categories, if you want them to be excluded from the home page of your blog.','custom_lang') .'
    
    ';
    }
    public function pre_get_posts(&$query) {
    if (!$query->is_admin && ($query->is_home || ($query->is_feed && !$query->is_category)) && (!isset($query->query_vars['suppress_filters']) || false === $query->query_vars['suppress_filters']) && !$query->is_preview) {
    if(get_option('excluded_categories') != '') {
    $excluded = get_option('excluded_categories');
    $excluded_arr = explode(',',$excluded);
    $query->set('category__not_in', array_map('intval',$excluded_arr));
    }
    }
    }
    }
    $custom_exclude_categories = new custom_exclude_categories ;
    /* END CATEGORY EXCLUDE CLASS */
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter keithwjones

    (@keithwjones)

    I am now thinking that the blank echo line is the problem
    and it needs something like:

    
    echo '<input type="text" name="excluded_categories" id="excluded_categories" value=">
    
    ';
    

    Keith

    • This reply was modified 5 years, 8 months ago by keithwjones.
    Thread Starter keithwjones

    (@keithwjones)

    Seems to function with this instead of the blank echo.

    
    echo '<input type="text" name="excluded_categories" id="excluded_categories">
    
    ';
    

    Keith

    Thread Starter keithwjones

    (@keithwjones)

    Right the code is now amended and appears to work:

    
    /* CLASS CATEGORY EXCLUDE FROM HOME and paged pages 
    excludes selected categories from main loop on the blog home page
    controlled by text input under dashboard->settings->reading -> 'Exclude Categories from Home'  */
    
    class custom_exclude_categories {
        public function custom_exclude_categories() {
            $this->__construct();
        }
        public function __construct() {
            add_action('admin_init',array($this,'register_setting'),10,0);
            add_action('admin_menu', array($this,'admin_option_setup'),10,0);
            add_action('pre_get_posts',array($this,'pre_get_posts'),20,1);
        }
        public function register_setting() {
            register_setting('reading','excluded_categories');
        }
        public function admin_option_setup() {
            add_settings_field( 'excluded_categories_field', __('Exclude Categories from Home', 'custom_lang'), array($this,'excluded_categories_field'), 'reading', 'default' );
        }
        public function excluded_categories_field() {
            $value = get_option('excluded_categories');
            echo '<p><input type="text" class="large-text code" id="excluded_categories" name="excluded_categories" value="'. $value .'" /></p>';
            echo '<p class="description">'. __('Enter here comma separated IDs of the categories, if you want them to be excluded from the home page of your blog.','custom_lang') .'</p>';
        }
        public function pre_get_posts(&$query) {
            if (!$query->is_admin && ($query->is_home || ($query->is_feed && !$query->is_category)) && (!isset($query->query_vars['suppress_filters']) || false === $query->query_vars['suppress_filters']) && !$query->is_preview) {
                if(get_option('excluded_categories') != '') {
                    $excluded = get_option('excluded_categories');
                    $excluded_arr = explode(',',$excluded);
                    $query->set('category__not_in', array_map('intval',$excluded_arr));
                }
            }
        }
    }
    $custom_exclude_categories = new custom_exclude_categories ;
    /* END CATEGORY EXCLUDE CLASS */
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Exclude Categories from Home page’ is closed to new replies.