将下面的代码放入functions.php 文件:
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\' );
 确保您注意代码中的注释,因为您可以将类别名称与其链接一起输出,也可以不输出。
如果要打印任何其他帖子的类别,在帖子之外,上述代码还支持以下格式:[category id=1234].