Skip to Content
Menu

post_categories()

Show the post categories.

PHP March 25, 2020

Usage

PHP
nebula()->post_categories($options)

Parameters

$options
(Optional) (Array) An array of options
Default: None

Parameter Notes

label
Whether to show the category “icon”, “text”, or not
Default: icon

linked
Whether to wrap the categories in a hyperlink
Default: true

show_uncategorized
Include the “Uncategorized” category
Default: true

force
Override the Customizer setting
Default: false

string
Return a string with no markup
Default: false

Request or provide clarification »

Demo


Using post_meta:
Using individual Nebula post meta functions with options:
  • Posted in PHP by
  • Modified on
  • Custom Format:
Other Nebula meta functions:
  • Function
  • No EXIF data found
Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Functions.php on line 761.

    2 Hooks

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_post_categories"
    "nebula_post_categories_defaults"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function post_categories($options=array()){
                $override = apply_filters('pre_nebula_post_categories', null, $options);
                if ( isset($override) ){return $override;}
    
                $defaults = apply_filters('nebula_post_categories_defaults', array(
                    'id' => get_the_ID(),
                    'label' => 'icon', //"icon" or "text"
                    'linked' => true, //Link to category archive
                    'show_uncategorized' => true, //Show "Uncategorized" category
                    'force' => false,
                    'string' => false, //Return a string with no markup
                ));
    
                $data = array_merge($defaults, $options);
    
                if ( get_theme_mod('post_categories', true) || $data['force'] ){
                    $the_categories = get_the_category($data['id']);
    
                    if ( $the_categories && is_array($the_categories) ){
                        //Optionally filter out "Uncategorized"
                        if ( !$data['show_uncategorized'] ){
                            $the_categories = array_filter($the_categories, fn($cat) => $cat->slug !== 'uncategorized');
                        }
    
                        if ( empty($the_categories) ){return null;}
    
                        $label = '';
                        $cat_plural = ( count($the_categories) > 1 )? __('categories', 'nebula') : __('category', 'nebula');
    
                        if ( $data['label'] === 'icon' ){
                            $label = '<i class="nebula-post-categories-label fa-solid fa-list"></i> ';
                        } elseif ( $data['label'] === 'text' ){
                            $label = '<span class="nebula-post-categories-label">' . ucwords($cat_plural) . ' </span>';
                        }
    
                        //If we just want comma-separated plain text
                        if ( $data['string'] ){
                            $category_names = array_map(fn($cat) => $cat->name, $the_categories);
                            return implode(', ', $category_names);
                        }
    
                        //Otherwise output individual span tags
                        $category_items = '';
                        $cat_count = count($the_categories);
                        $current = 0;
    
                        foreach ( $the_categories as $cat ){
                            $cat_name = esc_html($cat->name);
                            $current++;
    
                            if ( $data['linked'] ){
                                $cat_link = get_category_link($cat->term_id);
                                $category_items .= '<span role="listitem"><a href="' . esc_url($cat_link) . '">' . $cat_name . '</a></span>';
                            } else {
                                $category_items .= '<span role="listitem">' . $cat_name . '</span>';
                            }
    
                            if ( $current < $cat_count ){
                                $category_items .= ', ';
                            }
                        }
    
                        return '<span class="posted-in meta-item post-categories" role="list">' . $label . $category_items . '</span>';
                    }
                }
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_post_categories', 'my_custom_post_categories', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_post_categories($null, $options){ //$null is required, but can be ignored
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_post_categories', '__return_false');