Skip Navigation

[Resolved] Category based layouts

This thread is resolved. Here is a description of the problem and solution.

Problem:

Display different layout depends on taxonomy terms.

Solution:

There isn't such kind of built-in feature, you might consider custom codes, for example:

https://toolset.com/forums/topic/category-based-layouts/#post-1361047

Relevant Documentation:

https://codex.wordpress.org/Function_Reference/has_term

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by davoodD-2 5 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1361023

We have a listings site with a custom post type for the listings that was created using toolset. This custom post type has two categories Sold and For Sale. We want to remove contact info when we move the listing from the For Sale category to the Sold category. We have created a separate layout to use for Sold listings but can not figure out how to do what we want to do using the post types categories. Would you please provide us some guidance how to do this?

#1361047

Hello,

There isn't such kind of built-in feature, you might consider custom codes, for example:
1) Create two different layouts:
- First layout: For post which assigned with For Sale category (ID: 123)
- Second layout: For post which assigned with Sold category (ID: 456)

Assign all posts of your custom post type to the first layout(ID: 123)

2) When user open a single post of your custom post type, use filter hook "get_layout_id_for_render" to trigger a PHP function, check if current post has term "Sold category", then return the Second layout's ID

For example:

add_filter('get_layout_id_for_render', function( $id, $layout ){
	// replace below values according to your own website settings.
    $for_sale_layout_id = 123;
    $sold_layout_id = 456;
	$sold_term_slug = 'sold';
	$taxonmy_slug = 'my-taxonomy-slug';
	
	$sold = has_term( $sold_term_slug, $taxonmy_slug, get_the_ID());
	if( $sold && $id == $for_sale_layout_id){
		$id = $sold_layout_id;
	}
	return $id;
}, 10, 2);

more help:
https://codex.wordpress.org/Function_Reference/has_term

#1365141

At this time I have not had a chance to test the proposed code. I will in the next few days though. I would like this thread to remain open until I confirm the code works for me.

Thank you.

#1365153

OK, as your request, I have marked this thread as waiting for feedback status, it will be remained open until your confirmation.

#1365159

The code provided resolved my issue. Thank you so much.