• I made a shortcode to display the current post category and subcategories (2 in this case). It works fine when the current post has 2 subcategories, the problem is when it has 1, more than 2, or none. How could I make it work for different number of subcategories? I know there’s an “if” statement, but I could not make it work. This is my code:

    function post_parent_category_slug_shortcode() {
    
    // get the current category ID
    $category_id = get_the_category(get_the_ID());
    
    // get the current category object
      
    $child = get_category($category_id[0]->term_id);
    
    // get it's parent object
    $parent = get_category($child->parent);
            
    // get it's second parent object
    $second_parent = get_category($parent->parent);
        
    echo ($child->name . ' ' . $parent->name . ' ' . $second_parent->name);
    
    }
    add_shortcode( 'post_parent_category_slug', 'post_parent_category_slug_shortcode');

    When the post has exactly 2 subcategories the name of the main category and 2 subcategories displays fine. When the post has a different number of subcategories than 2, it gives error. I know my coding skills are not great, but is there a way to fix this shortcode and make it work?

    Thanks in advance!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Alor Web

    (@jerrymayalor555)

    Hi @carlosvai,

    Please try this update code and let me know if this is the result that you are looking for.

    function post_parent_category_slug_shortcode() {
    
        // get the current category IDs
        $category_ids = wp_get_post_categories(get_the_ID());
    
        if (empty($category_ids)) {
            return 'No categories found';
        }
    
        // get the current category objects
        $categories = array_map('get_category', $category_ids);
    
        // Display the names of all categories
        $category_names = array_map(function ($category) {
            return $category->name;
        }, $categories);
    
        return implode(' ', $category_names);
    }
    
    add_shortcode( 'post_parent_category_slug', 'post_parent_category_slug_shortcode');
    

    If you any have questions and clarification, just let me know.

    Thanks.

    Thread Starter carlosvai

    (@carlosvai)

    @jerrymayalor555 thanks a lot!! I really appreciate your help.
    It is almost exactly what I needed!
    With your code I’m getting no errors, so this helps a lot
    !
    The only thing I’d need is that the return outputs the child name plus the parents (if any).

    Current code:

    • Post has category/subcategory/second subcategory: It returns second subcategory.
      Post has category/subcategory: It returns subcategory.
      Post has category: It returns category.

    What I need:

  • Post has category/subcategory/second subcategory: It returns category/subcategory/second subcategory.
    Post has category/subcategory: It returns category/subcategory.
    Post has category: It returns category.
  • Thanks once again for your time and knowledge.

    Alor Web

    (@jerrymayalor555)

    Here is the updated code that displays your needed output.

    function post_parent_category_slug_shortcode() {
    
        $category_ids = wp_get_post_categories(get_the_ID());
    
        if (empty($category_ids)) {
            return 'No categories found';
        }
    
        $categories = array_map('get_category', $category_ids);
    
        $category_names = array_map(function ($category) {
            return $category->name;
        }, $categories);
    
        return implode('/', $category_names);
    }
    
    add_shortcode( 'post_parent_category_slug', 'post_parent_category_slug_shortcode');
    

    Let me know if it works.

    Thanks

    Thread Starter carlosvai

    (@carlosvai)

    Thanks again @jerrymayalor555 !
    Still outputs only the last subcategory in the chain.
    Nevertheless, is something I can work with in case it can’t be accomplished.

    My code gave error, it was absolutely unusable, so I really appreciate your help!

    Thread Starter carlosvai

    (@carlosvai)

    I’ll share another solution that someone posted in case anyone else needs it

    function post_parent_category_slug_shortcode() {
        // Get the current category ID
        $category_id = get_the_category(get_the_ID());
    
        // Check if the category is assigned
        if (empty($category_id)) {
            return 'No categories assigned';
        }
    
        $category = $category_id[0];
    
        // Start with the current category name
        $category_names = [$category->name];
    
        // Traverse up the category hierarchy and add parent names
        while ($category->parent != 0) {
            $category = get_category($category->parent);
    
            // Check for WP_Error before proceeding
            if (is_wp_error($category)) {
                break;
            }
    
            array_unshift($category_names, $category->name);
        }
    
        // Join the category names with a space and return
        return implode(' ', $category_names);
    }
    add_shortcode('post_parent_category_slug', 'post_parent_category_slug_shortcode');

    Thanks again for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Shortcode to display WP category and subcategories ONLY if they exist’ is closed to new replies.