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?
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
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.
OK, as your request, I have marked this thread as waiting for feedback status, it will be remained open until your confirmation.
The code provided resolved my issue. Thank you so much.