Skip Navigation

[Resolved] Assign layout to a specific archive

This support ticket is created 7 years, 10 months 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 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Pacific/Pago_Pago (GMT-11:00)

This topic contains 8 replies, has 2 voices.

Last updated by matteoF 7 years, 10 months ago.

Assisted by: Riccardo Strobbia.

Author
Posts
#410316

I've assigned a layout to product categories archive. Now I need to assign another layout to a specific product category, I can see it in the archives list, I assign it and save but that category displays first layout again

#410366

Dear Matteo,

can you provide a Duplicator package of your website, so that we can debug locally, backtrace the behaviour and solve the issue?

I guess there is a routing problem, we may have a little bug there and this would be of great help.

Thanks in advance,
Riccardo

#410873

ok I realized to have custom taxonomies named equal to product categores. Unfortunately my problem still exists but I noticed that Toolset has no options to choose a specific category, tag or product category as it happens for Pages for which you can assign a general layout for all pages and another layout for one or more specific pages. There is another way to obtain what I looking for?

#410996

Dear Matteo,

Layouts architecture, especially regarding what concerns assignments, follows strictly what WordPress offers by default, thus you're right, it is possible to assign Layouts to an entire taxonomy archive but not to a taxonomy term.

Nevertheless this is possible through the API using "get_layout_id_for_render" filter in your functions.php file.

This is the most simple example showing how to assign a specific layout to a category term page:

/**
* Supposed 10 is the $layout_id of the layout you want to assign to a given term
* and the category you want to assign the layout to is 9
**/
add_filter('get_layout_id_for_render', function( $layout_id, $slug ) {

		if( is_category( '9' ) ){
			$layout_id = 10;
		}

		return $layout_id;
});

If you want to do the same for tags or custom taxonomies you can use a combination of conditionals and get_queried_object or even Woocommerce custom functions for products.

The logic to use to get the term page you're in is up to you, but the track to follow is definitely this one.

Hope this helps,
Riccardo

#411036

I tried your code but nothing returns. Maybe "is_category" works just for post categories and not for product categories?

I tried another method I found here:
https://toolset.com/forums/topic/set-specific-layout-on-specific-post-status/

I edited it in this way but I get functions.php crash and blank pages in backend and frontend:

function assign_layout_by_taxonomy_id( $id, $layout ){
        global $product;
        $preferred_layout = 7970; // my layout ID
        $control_layout = 7957; //just set to 0 if you don't have any layout set for your post/product
 
        if( $id === $control_layout ){
            return $preferred_layout;
        }
        return $id;
    }
add_filter('get_layout_id_for_render', 'assign_layout_by_taxonomy_id', 10, 2);
#411048

I'm trying to use this code

function assign_layout_by_post_status( $id, $layout ){
  
        global $product;
  
        $preferred_layout = 7970; // my preferred layout ID
  
        $control_layout = 7957; //just set to 0 if you don't have any layout set for your post yet
 
if ( is_product_category( 'produttori' ) ) {
        if( $id === $control_layout ){
            
                return $preferred_layout;
            
        }
        return $id;
    }
}
add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

it seems to work for product category 'produttori' but return blank pages on others categories

#411049

Dear Matteo,

yes the example worked for default categories not for custom taxonomies, to be honest I don't see the relation your code has with the queried product category.

To check what is the current term queried in the product category taxonomy, I suggest to check the queried_object, something like:

add_filter('get_layout_id_for_render', function( $layout_id, $slug ) {
	
		if( is_post_type_archive( 'product_category' ) ){
			
			$taxonomy = get_queried_object();

  			if( $taxonomy->term_id === 10 ){
  				$layout_id = 10;
  			}
			
		}

		return $layout_id;
});

Take a look here: hidden link
and here: https://codex.wordpress.org/Function_Reference/get_queried_object.

Take my example as pseudo code and beware of using it directly without double check it and test it: it should be taken as proof of concept only and integrated with what you find in codex.

Hope this helps,
Riccardo

#411283

Sorry but this doesn't work for me, I tried to use other code because it used a switch between the layout assigned to all product categories and the specific one, getting the correct switch for it but going crash all others

using your code I keep getting on the same lyout assigned to all product categories

#411299

I resolved with the other code, there was a " } " more.

function assign_layout_by_post_status( $id, $layout ){
   
        global $product;
   
        $preferred_layout = 7970; // my preferred layout ID
   
        $control_layout = 7957; //just set to 0 if you don't have any layout set for your post yet
  
 if ( is_product_category( 'produttori' ) ) {
        if( $id === $control_layout ){
             
                return $preferred_layout;
             
        } 
 }

        return $id;
                                                     }

add_filter('get_layout_id_for_render', 'assign_layout_by_post_status', 10, 2);

now al product categories get the assigned layout '7957'
the category 'produttori' gets custom layout '7070'

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.