get parent category only

Try this: $post = get_post(); // If $post is already available, skip. $terms = get_the_terms( $post->ID, ‘category’ ); foreach ( $terms as $term ) : if ( $term->parent === 0 ) : echo ‘<a href=”‘ . esc_url( get_term_link( $term->term_id, ‘category’ ) ) . ‘” title=”‘ . esc_html( $term->name ) . ‘” ‘ . ‘>’ . … Read more

How do I add Category Body Classes to Admin?

You’re using $post->ID which is undefined. Try $_GET[‘post’] instead: // Get Categories if ( is_admin() && isset($_GET[‘post’]) ) { $cats = wp_get_post_categories( (int)$_GET[‘post’], array( ‘fields’ => ‘all’ ) ); $cats = wp_list_pluck( $cats, ‘slug’ ); foreach ( $cats as $cat ) { $classes .= ‘ category-‘ . $cat; } }

Show subcategories on a parent category page

This code checks if you’re on a category page, and if so it lists all the categories that are children of the current category. if (is_category()) { $this_category = get_category($cat); if (get_category_children($this_category->cat_ID) != “”) { wp_list_categories(‘orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of=”.$this_category->cat_ID); } }

list categories based on updated posts [closed]

WordPress, by default, doesn’t keep in database values for the last time a taxonomy term was updated. So, in order to achieve your goal, one solution is to loop through your posts and order them by date or by last modified. During the loop process you can store in a variable the term/category assigned to … Read more

Output category color option to each li item

what is stored in the term meta? a full hex color code like #123edf? your $color line seems to be inserted too early, and needs to be adapted; try: function the_category_colors() { $categories = get_the_category(); $separator=””; $output=””; if($categories){ foreach($categories as $category) { $output .= ‘<div class=”cat-span” style=”background-color: ‘. get_term_meta($category->term_id, ‘color’, true) . ‘; “><a href=”‘.get_category_link($category->term_id … Read more

Include WooCommerce product to all child categories

(1) Paste below code to your functions.php file function my_enqueue() { wp_enqueue_script( ‘my_custom_script’, get_template_directory_uri() . ‘/js/myscript.js’ ); } add_action( ‘admin_enqueue_scripts’, ‘my_enqueue’ ); (2) Create myscript.js file and paste below code in it and place the file in ‘yourtheme/js/’ folder. jQuery(document).ready(function () { jQuery(‘#categorychecklist’).prepend(‘<input type=”checkbox” id=”selectall”><label for=”selectall”>Select all subcategories.</label><hr>’); jQuery(‘#categorychecklist [type=”checkbox”]:not(#selectall)’).on(‘change’, function () { if(jQuery(‘#selectall’).is(‘:checked’)) { … Read more