Skip to Content
Menu

post_type()

Show the post type metadata.

PHP December 9, 2020

Usage

PHP
nebula()->post_type($icon)

Parameters

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

Parameter Notes

Option parameters include:

icon (mixed)
True for default icons, False to disable icons, or a string of class names to be used on an <i> tag.
Default: true

linked (boolean)
True links to the post type archive, false does not link
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 712.

    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_type"
    "nebula_post_type_defaults"
    Need a new filter hook? Request one here.

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

    PHP
            public function post_type($options=array()){
                $override = apply_filters('pre_nebula_post_type', null, $options);
                if ( isset($override) ){return $override;}
    
                $defaults = apply_filters('nebula_post_type_defaults', array(
                    'icon' => true, //True for generic defaults, false to disable icon, or string of class name(s) for icon.
                    'linked' => false //True links output to the post type archive page
                ));
                $data = array_merge($defaults, $options);
                $post_icon_img = false;
    
                if ( get_theme_mod('search_result_post_types', true) ){
                    $post_type = get_post_type();
                    $post_type_object = get_post_type_object($post_type);
                    $post_type_labels = $post_type_object->labels;
    
                    if ( $data['icon'] ){
                        $post_icon = ( $post_type_object->menu_icon )? $post_type_object->menu_icon : '';
                        $post_icon_img = '<i class="fa-solid fa-thumbtack"></i>';
    
                        if ( !empty($post_icon) ){
                            if ( str_starts_with($post_icon, 'http') || str_starts_with($post_icon, '/') ){
                                $post_icon_img = '<img src="' . esc_url($post_icon) . '" alt="' . esc_attr($post_type) . ' icon" style="width: 16px; height: 16px;" loading="lazy" />';
                            } elseif ( str_starts_with($post_icon, 'dashicons-') ){
                                $post_icon_img = '<i class="dashicons ' . esc_attr($post_icon) . '"></i>';
                            }
                        }
    
                        if ( gettype($data['icon']) === 'string' && $data['icon'] !== '' ){
                            $post_icon_img = '<i class="' . esc_html($data['icon']) . '"></i>';
                        }elseif ( $post_type === 'post' ){
                            $post_icon_img = '<i class="fa-solid fa-thumbtack"></i>';
                        } elseif ( $post_type === 'page' ){
                            $post_icon_img = '<i class="fa-solid fa-file-alt"></i>';
                        }
                    }
    
                    if ( $data['linked'] ){
                        return '<span class="meta-item post-type"><a href="' . esc_url(get_post_type_archive_link($post_type)) . '" title="See all ' . esc_attr($post_type_labels->name) . '">' . $post_icon_img . esc_html($post_type_labels->singular_name) . '</a></span>';
                    }
    
                    return '<span class="meta-item post-type">' . $post_icon_img . esc_html($post_type_labels->singular_name) . '</span>';
                }
    
                return '';
            }
    

    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_type', 'my_custom_post_type', 10, 2); //The last integer must be 1 more than the actual parameters
    function my_custom_post_type($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_type', '__return_false');