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
MB Favorite Posts in Bricks

MB Favorite Posts in Bricks

This tutorial explores using MB Favorite Posts in Bricks, including outputting the list of favorites via a query loop.
Categories:
Pro
Meta Box Group’s Number Sub Field Values Sum

Meta Box Group’s Number Sub Field Values Sum

In the Bricks Facebook group a user asked: We can define a custom function that takes a Meta Box group ID, the sub field ID…
Categories:
Tags:
Pro
Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

This Pro tutorial provides the steps for setting up a cloneable Meta Box group query inside a CPT query in Bricks. Scenario: CPT: course Meta…
Categories:
Pro
Dynamic Accordion in WordPress using Meta Box and Alpine.js

Dynamic Accordion in WordPress using Meta Box and Alpine.js

This Pro tutorial provides the steps to implement an accordion using Alpine.js in WordPress that pulls the values of sub fields of a Meta Box…
Pro
Dynamic Source for Video Element in Bricks Using Meta Box Post Field

Dynamic Source for Video Element in Bricks Using Meta Box Post Field

This Pro tutorial provides the steps to set the source of a Bricks video element on single CPT pages to be the URL of a…
Categories:
Pro
Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

Filtering Cloneable Meta Box Group by Sub Field Value in Bricks

This Pro tutorial provides the steps to filter the rows of a Meta Box cloneable group to only those that have a date custom field…
Pro
Querying Posts by Date/Datetime Custom Fields in Bricks

Querying Posts by Date/Datetime Custom Fields in Bricks

This Pro tutorial shows how to configure ACF and Meta Box to query posts in Bricks by custom field(s) of the type Date/Date Time Picker…
Categories:
Tags: