Clicky

  1. Home
  2. Developer Docs
  3. General
  4. 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).

add_filter('amp_post_template_file', 'ampforwp_custom_layout_for_categories', 15, 3 );
function ampforwp_custom_layout_for_categories( $file, $type, $post ){
$cat = wp_get_post_categories($post->ID);
if ( is_single() ) {
if ( $cat[0] === 7 ) {
$file = 'design.php';
}
}
return $file;
}


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.

$cat = wp_get_post_categories($post->ID);

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.

if ( is_single() ) {

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’

if ( $cat[0] === 7 ) {

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

$file = 'design.php';

“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.

Example:

add_filter('amp_post_template_file', 'our_new_func', 15, 3 );
function our_new_func( $file, $type, $post ){
$cat = wp_get_post_categories($post->ID);
if ( is_single() ) {
if ( $cat[0] === 7 ) {
$file = 'design.php';
}
if ( $cat[0] === 5 ) {
$file = 'design_1.php';
}
if ( $cat[0] === 9 ) {
$file = 'design_2.php';
}
if ( $cat[0] === 12 ) {
$file = 'design_3.php';
}
}
return $file;
}


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 May 6, 2020

    Are you still looking for a solution?

    Then fill out the below form and we will reach out to you within a few hours.

    Was this article helpful?

    Related Articles

    Leave a Comment