1. Home
  2. Developer Docs
  3. Change AMP Layout Based on Categories for Single Post

Change AMP Layout Based on Categories for Single Post

In this post, I’ll be showing you how we can modify our AMP for WP Accelerated Mobile Pages functionality to change the Single Post layout based on their Category.

Let’s get down to it…

Following is the code that you can either include in the child plugin or the core plugin (AMP for WP Accelerated Mobile Pages).

There goes the code, Now It’s time to break it in pieces and understand how does it work so that you can make the code work with your requirements.

First thing first the function “ampforwp_custom_layout_for_categories”  you can name as per your liking, takes three parameters, “$file, $type, $post” which contains all the information we need.

for example, we’ll be modifying the $file for changing the layout with our custom layout.

In our function first, we’ll get the category to which the post belongs to with the help of the following code.

now $cat an array of category ids.

We decided to run execute this only on Single Post so there will be a condition to check it.

we’ll check for other condition now that is to execute only for a specific category say the category we wanted to change the layout for has the ID ‘7’

and if this condition held true then we only need to modify the $file with our custom file.

“design.php” is my file, you can have there your file that you wanted to execute for the specific Category.

[Note]: make sure that all the brackets are closed and your php file should not have any errors otherwise it might break your AMP site but not the actual site.

You can place as many as conditions for different categories and can select a different layout for them.


So that’s it guys, with this you can change any layout to any category and can expand your AMP user’s satisfaction.

That’s it for this tutorial!

We hope it helped you. If you have any feedback or questions, then please share them in the comments section below.

Ask your Questions Directly!
If you can't find the solutions in this article, then just ask us in the comments and we will answer it for you!
Updated on February 3, 2020

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment