Custom Fields
Re-Envisioned
Support MB Term Meta Getting Color From Taxonomy

  • This topic has 2 replies, 2 voices, and was last updated 1 month ago by Vlhh.
  • Creator
    Topic
  • #31664
    Resolved Vlhh
    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #31671
    Long 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
    Vlhh
    Participant

    Awesome, that worked.

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

    Thank you!

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