Custom taxonomies terms on static page (frontpage)

by aammee   Last Updated March 02, 2018 12:08 PM

how to get custom terms on static page (frontpage) in wordpress theme uses this code but this show only builtin taxonomies terms but not custom taxonomies terms with get_category(); }?> how to get custom terms on frontage please help me thanks in advance

 function get_category() {
   $buffy = '';
    $selected_category_obj = '';
    $selected_category_obj_id = '';
    $selected_category_obj_name = '';

    $current_post_type = get_post_type($this->post->ID);
    $builtin_post_types = get_post_types(array('_builtin' => true));

    if (array_key_exists($current_post_type, $builtin_post_types)) {

        // default post type

        //read the post meta to get the custom primary category
        $td_post_theme_settings = td_util::get_post_meta_array($this->post->ID, 'td_post_theme_settings');
        if (!empty($td_post_theme_settings['td_primary_cat'])) {
            //we have a custom category selected
            $selected_category_obj = get_category($td_post_theme_settings['td_primary_cat']);
        } else {

            //get one auto
            $categories = get_the_category($this->post->ID);

            if (is_category()) {
                foreach ($categories as $category) {
                    if ($category->term_id == get_query_var('cat')) {
                        $selected_category_obj = $category;
                        break;
                    }
                }
            }

            if (empty($selected_category_obj) and !empty($categories[0])) {
                if ($categories[0]->name === TD_FEATURED_CAT and !empty($categories[1])) {
                    $selected_category_obj = $categories[1];
                } else {
                    $selected_category_obj = $categories[0];
                }
            }
        }

        if (!empty($selected_category_obj)) {
            $selected_category_obj_id = $selected_category_obj->cat_ID;
            $selected_category_obj_name = $selected_category_obj->name;
        }

    } else {

        // custom post type

        // Validate that the current queried term is a term
        global $wp_query;
        $current_queried_term = $wp_query->get_queried_object();

        if ( $current_queried_term instanceof WP_Term ) {
            $current_term = term_exists( $current_queried_term->name, $current_queried_term->taxonomy );

            if ($current_term !== 0 && $current_term !== null) {
                $selected_category_obj = $current_queried_term;
            }
        }


        // Get and validate the custom taxonomy according to the validated queried term
        if (!empty($selected_category_obj)) {

            $taxonomy_objects = get_object_taxonomies($this->post, 'objects');
            $custom_taxonomy_object = '';

            foreach ($taxonomy_objects as $taxonomy_object) {

                if ($taxonomy_object->_builtin !== 1 && $taxonomy_object->name === $selected_category_obj->taxonomy) {
                    $custom_taxonomy_object = $taxonomy_object;
                    break;
                }
            }

            // Invalid taxonomy
            if (empty($custom_taxonomy_object)) {
                return $buffy;
            }

            $selected_category_obj_id = $selected_category_obj->term_id;
            $selected_category_obj_name = $selected_category_obj->name;
        }
    }

    if (!empty($selected_category_obj_id) && !empty($selected_category_obj_name)) { //@todo catch error here
        $buffy .= '<a href="' . get_category_link($selected_category_obj_id) . '" class="td-post-category">'  . $selected_category_obj_name . '</a>' ;
    }

    //return print_r($post, true);
    return $buffy;
}


Related Questions


Why does wp_remove_object_terms not work?

Updated April 01, 2018 06:08 AM



Custom Taxonomy Archive BUG

Updated April 25, 2017 21:08 PM