• Resolved RickHN

    (@rchinchilla)


    Hi this is a great plugin!

    I’m wondering if its possible to get the post’s category name in similar way as can do it for post titles?

    I’ll appreciatte some answer here, thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Yes! You can do that with a custom shortcode like this:

    function get_post_category()
    {
        $categories = wp_get_post_categories(
            0,
            array('fields' => 'names')
        );
        if (is_array($categories)) {
            return implode(', ', $categories);
        }
        return '';
    }
    add_shortcode('get_post_category', 'get_post_category');

    And then you can use your custom shortcode with DTX like so:

    [dynamictext inputname "get_post_category"]

    And that should provide you with a comma separated list of category names in the text field. Let me know if this helps!

    Thread Starter RickHN

    (@rchinchilla)

    Thanks for answer.

    I tried the shortcode snippet but only displaying an empy field.

    What I’m trying is to get the category name(s) for the currently post on screen (where CF7 form is active).

    Info: I’m trying within CPT “wiki”, and just confirmed these posts has categories assigned.

    Some idea, please?

    • This reply was modified 2 years ago by RickHN.
    • This reply was modified 2 years ago by RickHN. Reason: more explicit
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Ah, I reread the documentation for wp_get_post_categories() and the first parameter does not automatically default to the ID of the global $post object, so you’ll need to actually pass the post ID. Try modifying that snippet to this:

    function get_post_category()
    {
        global $post;
        $categories = wp_get_post_categories(
            $post->ID,
            array('fields' => 'names')
        );
        if (is_array($categories)) {
            return implode(', ', $categories);
        }
        return '';
    }
    add_shortcode('get_post_category', 'get_post_category');

    That’s what I get for trying to write code without testing lol

    Thread Starter RickHN

    (@rchinchilla)

    Thanks for quickly response.

    I’m afraid that still not working yet.

    Not working even if I replace $post->ID with a specific number of existent post ID.

    Same results… empty field.

    ??

    • This reply was modified 2 years ago by RickHN.
    • This reply was modified 2 years ago by RickHN.
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Okay, give me a bit longer to figure out the code ??

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Thank you for your patience!

    I was able to get it to work with the wp_get_post_categories() function but I wrote this shortcode in my active theme’s functions.php file to be flexible with custom taxonomies as well, just in case.

    function au_get_post_categories()
     {
        $post_id = 0;
        global $post;
        if (isset($post)) {
            $post_id = $post->ID; // If the global $post object is set, get its ID
        } else {
            $post_id = get_the_ID(); // Otherwise get it from "the loop"
        }
        if ($post_id) {
            $categories = wp_get_object_terms(
                $post_id, // Get only the ones assigned to this post
                'category', // Or a custom taxonomy
                array('fields' => 'names') // Return an array of term names
            );
            if (is_array($categories) && count($categories)) {
                // Return a string value for the text field input
                return esc_attr(implode(', ', $categories));
            }
            return 'Categories not an array or is empty'; // Comment this out (or remove) when done testing
        }
        return 'Post ID could not be set'; // Return an empty string when done testing
    }
    add_shortcode('au_get_post_categories', 'au_get_post_categories', 10, 0);

    And then in my contact form, I used this form tag:

    [dynamictext post_categories "au_get_post_categories"]

    As intended, it returned a comma delimited list of category names assigned to the current post the form was located on in this text field.

    This was tested in my local WP environment on a native post type using WordPress’s native category taxonomy. Even if I unchecked all the categories while editing the post and then saved it, it defaulted to being assigned the “Uncategorized” category, so my two testing strings “Post ID could not be set” and “Categories not an array or is empty” never returned because the native WordPress behavior always ensures that every post has at least one category. Of course, themes, plugins, and other devs may override that so I have the checks just in case ?? Just be sure to remove the debugging language (noted in code comments) before using it in a production environment.

    Cheers!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can I get the category label/name of current post?’ is closed to new replies.