Change color style for posts which have a "CUSTOM taxonomy"

Support MB Term Meta Change color style for posts which have a "CUSTOM taxonomy"Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #34447
    ValentinoValentino
    Participant

    Hello,
    I saw this tutorial, and it worked great when i use it with post categories:
    https://metabox.io/wordpress-style-posts-in-particular-category/

    But I wanted to create a custom taxonomy and use it as "theme color style".
    so I created a taxonomy called "post style color" and added the custom fields to that taxonomy as shown in the tutorial.
    Added this code:

    <div>
    function estar_child_output_frontend() {
        If ( ! is_single() ) {
            return;
        }
        $categories       = get_the_category();
        $background_color = get_term_meta( $categories[0]->term_id, 'background_color_taxonomy', true );
        if ( ! $background_color ) {
            return;
        }
        echo '<style>
            .single-post {
                background-color: ' . $background_color . ';
            }
        </style>';
    }
    add_action( 'wp_head', 'estar_child_output_frontend' );
    </div>

    But it doesnt work with custom taxonomies but only with wp categories, why?

    #34457
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can change the code to get the term meta

    $categories = get_the_category();
    $background_color = get_term_meta( $categories[0]->term_id, 'background_color_taxonomy', true );

    to this one

    $term_id = get_queried_object_id();
    $background_color = rwmb_meta( 'background_color_taxonomy', ['object_type' => 'term'], $term_id );

    refer to the documentation https://docs.metabox.io/extensions/mb-term-meta/#getting-field-value

    #34459
    ValentinoValentino
    Participant

    Hi Long, Thanks for your reply. I changed those 2 line code but it doen't work
    the <style> doesn't get to the front end.
    As I said it worked with WP categories but \it doesn't work with custom taxonomy.

    This is the custom taxonomy I've made:

    <div>
    <?php
    add_action( 'init', 'your_prefix_register_taxonomy' );
    function your_prefix_register_taxonomy() {
        $labels = [
            'name'                       => esc_html__( 'post style color', 'your-textdomain' ),
            'singular_name'              => esc_html__( 'post style color', 'your-textdomain' ),
            'menu_name'                  => esc_html__( 'Post style color', 'your-textdomain' ),
            'search_items'               => esc_html__( 'Search post style color', 'your-textdomain' ),
            'popular_items'              => esc_html__( 'Popular post style color', 'your-textdomain' ),
            'all_items'                  => esc_html__( 'All post style color', 'your-textdomain' ),
            'parent_item'                => esc_html__( 'Parent post style color', 'your-textdomain' ),
            'parent_item_colon'          => esc_html__( 'Parent post style color', 'your-textdomain' ),
            'edit_item'                  => esc_html__( 'Edit post style color', 'your-textdomain' ),
            'view_item'                  => esc_html__( 'View post style color', 'your-textdomain' ),
            'update_item'                => esc_html__( 'Update post style color', 'your-textdomain' ),
            'add_new_item'               => esc_html__( 'Add new post style color', 'your-textdomain' ),
            'new_item_name'              => esc_html__( 'New post style color name', 'your-textdomain' ),
            'separate_items_with_commas' => esc_html__( 'Separate post style color with commas', 'your-textdomain' ),
            'add_or_remove_items'        => esc_html__( 'Add or remove post style color', 'your-textdomain' ),
            'choose_from_most_used'      => esc_html__( 'Choose most used post style color', 'your-textdomain' ),
            'not_found'                  => esc_html__( 'No post style color found', 'your-textdomain' ),
            'no_terms'                   => esc_html__( 'No post style color', 'your-textdomain' ),
            'filter_by_item'             => esc_html__( 'Filter by post style color', 'your-textdomain' ),
            'items_list_navigation'      => esc_html__( 'Post style color list pagination', 'your-textdomain' ),
            'items_list'                 => esc_html__( 'Post style color list', 'your-textdomain' ),
            'most_used'                  => esc_html__( 'Most Used', 'your-textdomain' ),
            'back_to_items'              => esc_html__( 'Back to post style color', 'your-textdomain' ),
        ];
        $args = [
            'label'              => esc_html__( 'Post style color', 'your-textdomain' ),
            'labels'             => $labels,
            'description'        => '',
            'public'             => true,
            'publicly_queryable' => true,
            'hierarchical'       => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'show_in_nav_menus'  => false,
            'show_in_rest'       => true,
            'show_tagcloud'      => false,
            'show_in_quick_edit' => true,
            'show_admin_column'  => false,
            'query_var'          => true,
            'sort'               => false,
            'meta_box_cb'        => 'post_tags_meta_box',
            'rest_base'          => '',
            'rewrite'            => [
                'with_front'   => false,
                'hierarchical' => false,
            ],
        ];
        register_taxonomy( 'post-style-color', ['post'], $args );
    }
    </div>

    This is the custom fields I made:

    '<div>
    <?php
    add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
    
    function your_prefix_function_name( $meta_boxes ) {
        $prefix = '';
    
    $meta_boxes[] = [
        'title'      => __( 'Color style Taxonomy', 'your-text-domain' ),
        'id'         => 'color-style-taxonomy',
        'taxonomies' => ['post-style-color', 'category'],
        'fields'     => [
            [
                'name'    => __( 'Background Color', 'your-text-domain' ),
                'id'      => $prefix . 'background_color_taxonomy',
                'type'    => 'color',
                'columns' => 3,
            ],
        ],
    ];
    
    return $meta_boxes;
    }
    </div>'

    This is the dode I put in the fuctions.php:

    <div>
    // MetaBox color style taxonomy
    function astr_child_output_frontend() {
        If ( ! is_single() ) {
            return;
        }
        $term_id = get_queried_object_id();
    $background_color = rwmb_meta( 'background_color_taxonomy', ['object_type' => 'term'], $term_id );
        if ( ! $background_color ) {
            return;
        }
        echo '
    <!-- custom style taxonomy -->
        <style>
            .single-post {
                --bg-color1: ' . $background_color . ';
    
        }
    </style>';
    }
    add_action( 'wp_head', 'astr_child_output_frontend' );
    
    </div>

    Do you know why it doesn't work?

    #34473
    Long NguyenLong Nguyen
    Moderator

    Hi,

    On the single post, please change the code to get the background color to this one

    function astr_child_output_frontend() {
        If ( ! is_single() ) {
            return;
        }
        $post_id = get_queried_object_id();
        $term_colors = get_the_terms( $post_id, 'post-style-color' );
        $background_color = rwmb_meta( 'background_color_taxonomy', ['object_type' => 'term'], $term_colors[0]->term_id );
        if ( ! $background_color ) {
            return;
        }
        echo '
    <!-- custom style taxonomy -->
        <style>
            .single-post {
                background-color: ' . $background_color . ';
    
        }
    </style>';
    }
    add_action( 'wp_head', 'astr_child_output_frontend' );
    
    #34479
    ValentinoValentino
    Participant

    Hi Long,
    it seems to work now.
    Thank so much for your help!

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