Documentation › Auto-assign layouts by category

Auto-assign layouts by category

Something that is requested on a regular basis is the ability to auto-assign posts a shared layout based on the category of the post. Previously, there’s been no way to do this but as of 3.8.3, there is! So let’s get started!

Step 1: You need a child theme

See this article for more on child themes.

 Step 2: Choose category of posts (or CPT) you will auto-assign layouts to

You need to know the ID, the slug, or the title of the category you want.  I would recommend using ID just because that typically stays constant and you may edit the category slug/title later.

To find the category ID, open up the category admin page in WordPress.  Now you can either hover over the edit link long enough that the ID shows in the status bar


Or you can click edit and find the ID in your browser address bar


Step 3: Get the slug of the shared layout you want to assign

Edit the Shared Layout you want to auto-assign by category in the Visual Editor.  In the address bar, you’ll find the slug we need.


Step 4: Copy and paste the necessary code into your child theme functions.php

So what does this code do? The first line connects our function to the hook created to do this. Easy enough.

Then we create our function. We need the $current_layout variable in the function.

if(is_single()) {

This first checks if a single post of any post type (except attachment and page post types) is being displayed.

if(in_category(14)) {

This code using a WordPress conditional to check whether or not the post is in the category with the ID of 14. For more information on in_category, see the WordPress Documentation

 $current_layout[] = 'template-1';

This sets the $current_layout variable to the slug of our shared layout we want to auto-assign.

Finally, we return the variable using:

return $current_layout;

This article was last updated for Headway version 3.8.3

Copyright © 2016 Vesped Inc. All Rights Reserved. Proudly Powered by Headway and WordPress