• Hi everyone,

    First thanks for this really useful plugin. I have some troubles to get it works – maybe due to me.

    I am using WP rest json api and I would like to add a ‘color’ field when requesting categories.

    The code doing this is really simple :

    function get_category_color( $object, $field_name, $request ) {
    	return get_term_meta($object['id'], ‘cc_color’, true);
    }

    Here, get_term_meta exists (checked with function_exists) and $object['id'] is my category id.

    The table seems also good :

    MariaDB [wordpress]> select * from wp_termmeta;
    +---------+---------+----------+------------+
    | meta_id | term_id | meta_key | meta_value |
    +---------+---------+----------+------------+
    |       1 |       5 | cc_color | #e5a629    |
    |       2 |       3 | cc_color | #ff0245    |
    |       3 |       2 | cc_color | #FF5B84    |
    |       4 |       1 | cc_color | #8B80FE    |
    |       5 |       6 | cc_color | #D50184    |
    |       6 |       4 | cc_color | #850400    |
    |       7 |       5 | cc_color | #e5a629    |
    |       8 |       5 | cc_color | #e5a629    |
    +---------+---------+----------+------------+

    But the ‘color’ fields is always an empty string. Of course, I’ve tried to check my setup by returning directly a string and it works great.

    I don’t really know what to do (I’m not really use to work with wp).

    Thanks a lot !

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Gesundheit Bewegt GmbH

    (@loyaltymanufaktur)

    Hello mildful,

    you should use object term_id instead of id to get the colour value.

    Best regards,
    Loyalty Manufaktur

    Thread Starter mildful

    (@mildful)

    Thanks for your answer.

    Unfortunatly, I’ve already tried this solution, maybe I do it wrong :

    
    function get_category_color( $object, $field_name, $request ) {
    	return get_term_meta(get_term($object['id'], $object['taxonomy'])->term_id, ‘cc_color’, true);
    }
    

    I’ve also tried by directly giving the term_id (as an int) but the behavior is the same.

    If it may help you, Mildful, i made the following function inside my themes functions.php to grab the color code for the category.

    function insert_post_color() {
        global $post;
        $postcat = get_the_category( $post->ID );
    
        if ( ! empty( $postcat ) ) {
            $post_color = get_term_meta( $postcat[0]->cat_ID, 'cc_color', true);
        } else {
            $post_color = '#00659F'; //Return a default or fallback color if no colors exist
        }
        echo $post_color;
    }

    Usage: Post the following code anywhere between php tags in a post, page or custom posttype which has categories. The function has a fallback if there aren’t any colors found.
    insert_post_color()

    Thread Starter mildful

    (@mildful)

    Hello Borneyak and thanks for your help !

    But I’m not sure to be able to use it since I use wordpress just as a REST API. My front is deserved by an Angular application.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Can’t retrieve color using rest api ?’ is closed to new replies.