Getting Color From Taxonomy

Support MB Term Meta Getting Color From TaxonomyResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #31664
    VlhhVlhh
    Participant

    I'm working on a posts template for Oxygen and trying to output a list of tags for the current post.

    The goal is to change the background colour of each tag by using the colour field post_tag_color that I have added to my posts->tags taxonomy with Metabox.

    This code (inside Oxygen's code block) only returns `style="background-color:;". when i'm looking at the html markup. The rwmb_meta doesn't output anything.

    <?php
    
    $post_tags = get_the_tags();
    $background_color = rwmb_meta( 'post_tag_color' );
    
    if ( $post_tags ) {
        foreach($post_tags as $tag) {
            echo "<span class=\"post-tags post-tags-id-$tag->term_id\" style=\"background-color:$background_color;\">$tag->name</span></style>";
    
        }
    }
    ?>

    What am I doing wrong?

    #31671
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To get the term meta, you have to pass the term object type and term ID to the helper function

    if ( $post_tags ) {
        foreach($post_tags as $tag) {
            $background_color = rwmb_meta( 'post_tag_color', ['object_type' => 'term'], $tag->term_id );
            echo "<span class=\"post-tags post-tags-id-$tag->term_id\" style=\"background-color:$background_color;\">$tag->name</span></style>";
    
        }
    }

    Please get more details on the documentation https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value

    #31672
    VlhhVlhh
    Participant

    Awesome, that worked.

    I tried something like that but my php skills are close to zero.

    Thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.