Opens in a new tab
5th Aug '23
/
2 comments

Meta Box Category Colors in Single Posts

Looking to use Meta Box Color Picker fields’ values in a single post Bricks template based on the post category?

This tutorial shows how.

Note: Designed to work mainly for the case where posts have a single category assigned to each.

Step 1

Create a field group named say, “Category Fields” having category_primary_color and category_secondary_color fields of the type, Color Picker.

Location: Category taxonomy.

Step 2

Edit your categories and pick colors for both the fields.

Step 3

Let’s define a custom function that returns the color based on the specified type, primary or secondary or, if the color is not set a fallback/default color.

Add the following in child theme‘s functions.php or a code snippets plugin:

// Avoid Undefined Function Error when Meta Box is not active.
if ( ! function_exists( 'rwmb_meta' ) ) {
	function rwmb_meta( $key, $args = '', $post_id = null ) {
			return false;
	}
}

// Custom function to get the value of the category color meta field.
function bl_get_category_color_in_post( string $type ): string {
		$color = rwmb_meta( "category_{$type}_color", [ 'object_type' => 'term' ], wp_get_post_categories( get_the_ID() )[0] );

		return $color ? $color : '#333'; // fallback color
}

Step 4

Edit your single post template with Bricks.

For setting the category primary color or category secondary color field’s value as the background color of an element, click on the color picker, then RAW and paste:

{echo:bl_get_category_color_in_post(primary)}

The above will set the value of category_primary_color field as the background color.

For secondary, you’d instead do:

{echo:bl_get_category_color_in_post(secondary)}

You can similarly set one of these two fields’ values as Color if you wish.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

2 comments

Leave your comment

 

Related Tutorials..

Pro
Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

Bricks Forms + Meta Box: Auto-Create Post Relationships on Form Submission

How to link a Bricks post-creation form on a post to a newly created post of another post type using Meta Box relationship.
Pro
Meta Box Taxonomy Field’s Image Value in Posts

Meta Box Taxonomy Field’s Image Value in Posts

In a project I am currently working on, the requirement is to show the image of an "Image Advanced" type attached to a taxonomy in…
Categories:
Tags:
Pro
Meta Box Relationship Posts Filtered by a Taxonomy Term in Bricks

Meta Box Relationship Posts Filtered by a Taxonomy Term in Bricks

Updated on 17 Feb 2025 In the Inner Circle a user asks: Hello, I have a relationship made in metabox between two Post Types “Travel”…
Categories:
Custom Image Field Avatar in WordPress

Custom Image Field Avatar in WordPress

Learn how to use a Meta Box image field value as the author profile image on single post pages in WordPress/Bricks.
Categories:
Tags:
Pro
Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

Filtering posts by a group field’s true / false field sub field value with ACF or Meta Box in Bricks

This Pro tutorial covers how posts of a Bricks query loop can be filtered to show only the ones whose true / false or checkbox…
Categories:
Tags:
Pro
Ordering Meta Box group rows by a sub field in Bricks

Ordering Meta Box group rows by a sub field in Bricks

In the BricksLabs Facebook group a user asks: Any idea how to set the sort order of a cloneable group in the query? In this…
Color Custom Field as Section Background Color on Category/Term Archives in Bricks

Color Custom Field as Section Background Color on Category/Term Archives in Bricks

How to use a color from a taxonomy term custom field for as the hero section background color on archive pages.
Categories:
Tags: