0

I want to show the category name of the post when I write [category] shortcode in the post body. I'm using this code but only showing array error.

Sorry, I'm not an expert in coding. Perhaps you can redefine my code? Please.

function cat_post() {
    return get_the_category();
}
add_shortcode('categorypost', 'cat_post');
  • 1
    You have set up [categorypost] rather than [category]. Also, get_the_category() will need to know what post you're asking about, so you need to specify global $post above and call get_the_category($post->ID). Finally, get_the_category() will return an array, so you shouldn't return it directly, you'll need to parse it and determine how you want to display the results. – WebElaine May 28 at 13:54
  • @WebElaine get_the_category() doesn't need post_ID - by default it will use global/current post. So it makes sense in this case - it is shortcode, which is used inside of post, so the global $post is set. – Krzysiek Dróżdż May 28 at 21:49
0

Put the code below in your functions.php file:

function register_theme_shortcodes() {
    add_shortcode( 'category',
        /**
         * category shortcode function.
         *
         * @param array $atts
         *
         * @return string
         */
        function ( $atts ) {
            $atts = shortcode_atts(
                array(
                    'id' => '',
                ),
                $atts
            );

            /**
             * @var int $id Optional Post ID to get categories from.
             */
            extract( $atts );

            if ( $id ) {
                $post_id = $id;
            } else {
                global $post;

                if ( $post instanceof WP_Post ) {
                    $post_id = $post->ID;
                }
            }

            if ( empty( $post_id ) ) {
                $output = '';
            } else {
                $output = [];

                if ( $post_categories = get_the_category() ) {
                    /**
                     * @var WP_Term $category
                     */
                    foreach ( $post_categories as $category ) {
                        // Builds the category name with its link.
                        $output[] = "<a href='" . get_term_link( $category->term_id ) . "'>{$category->name}</a>";

                        // Build just the category name.
                        // $output[] = $category->name;
                    }
                }

                $output = implode( ', ', $output );
            }

            return $output;
        }
    );
}

add_action( 'init', 'register_theme_shortcodes' );

Make sure you pay attention to the comments along the code as you can have the category names output with their links or not.

And if you want to print the category of any other post, outside the post, the code above also supports the following format: [category id=1234].

  • Thank you so much. It works perfectly as expected! – CDW May 29 at 13:36
0

The main problem with your code is that you register [categorypost] shortcode, but you want to register it as [category].

Another problem is that your shortcode should return HTML code - the output of shortcode. And as you can see here, the get_the_category function returns an array of terms.

So how to write it correctly?

The easiest way will be to use the_category() template tag. So here's the code:

function category_shortcode_callback( $atts ) {
    // set default params for the_category
    $a = shortcode_atts( array(
        'separator' => '',
        'parents' => '',
        'post_id' => false
    ), $atts );

    ob_start();  // start output buffering
    the_category( $a['separator'], $a['parents'], $a['post_id'] );  // print categories
    return ob_get_clean();  // grab the output and return it
}
add_shortcode( 'category', 'category_shortcode_callback' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.