Skip to main content

Conditional Layouts via Filters

warning

Conditional layout code can become a maze. Keep conditions explicit, document intent, and avoid overlapping rules.

Conditional Layouts via Filters Explained​

FilterWhat it changesExample condition
generate_sidebar_layoutSidebar placement/visibilityis_page( 'landing' )
generate_content_layout (example)Content container styleis_single()
body_classCSS targetingis_post_type_archive()

Why It Matters​

  • Different templates need different layouts (landing vs blog vs product).
  • Filters let you apply those decisions without template overrides.
  • This approach is version-controlled and reviewable.

How It Works​

GeneratePress calls apply_filters() during layout decisions. Your child theme returns the value you want based on the request context.

Practical Walkthrough​

Step 1: Find Layout Filters in GeneratePress​

find-layout-filters.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "apply_filters( 'generate_" -n inc/structure inc | head -n 80

Step 2: Implement a Filter in the Child Theme​

wp-content/themes/generatepress-child/inc/filters.php
<?php

add_filter( 'generate_sidebar_layout', function( $layout ) {
if ( is_page( 'landing' ) ) {
return 'no-sidebar';
}

return $layout;
} );

Step 2b: Find Valid Return Values (Optional)​

find-sidebar-layout-values.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "no-sidebar\|left-sidebar\|right-sidebar" -n inc 2>/dev/null | head -n 60 || true

Step 3: Keep a Condition Table for Your Site​

Page typeDesired layoutImplementation
Landing pagesNo sidebarFilter + specific page slug
Blog postsRight sidebarDefault or filter
CheckoutNo header/footerLayout element or filter

Practical Examples​

Example 1: No Sidebar on All Pages Using a Template​

If you use a dedicated page template, target it with is_page_template().

Example 2: Add a Body Class for Landing Pages​

wp-content/themes/generatepress-child/inc/filters.php
<?php

add_filter( 'body_class', function( $classes ) {
if ( is_page( 'landing' ) ) {
$classes[] = 'is-landing';
}
return $classes;
} );

Example 3: Conditional Layout for Products (If WooCommerce)​

wp-content/themes/generatepress-child/inc/filters.php
<?php

add_filter( 'generate_sidebar_layout', function( $layout ) {
if ( function_exists( 'is_shop' ) && is_shop() ) {
return 'no-sidebar';
}
return $layout;
} );

Best Practices​

PracticeWhy
Keep conditions narrowPrevents unexpected layouts
Prefer one rule per intentEasier debugging
Document return valuesAvoid magic strings
Test logged-outCaching differences
Keep a rule inventoryPrevents overlap
Prefer IDs for critical pagesSlugs can change

Troubleshooting​

SymptomCauseFix
Filter has no effectWrong filter nameConfirm with grep in the parent theme
Layout changes randomlyOverlapping rulesConsolidate conditions and document
Changes not visibleCachePurge LSCache

Hands-On​

  1. Find two layout-related filters in GeneratePress.
  2. Implement one condition (no sidebar on one page).
  3. Add a body class and write a small CSS rule for that class.
  4. Add one more condition (product/shop or archive) and confirm no overlap.
  5. Write a short rule table in README (page -> layout -> implementation).

Quick Reference​

conditional-layouts-cheatsheet.sh
cd /var/www/html/wp-content/themes/generatepress
grep -R "apply_filters( 'generate_" -n inc/structure | head

What's Next​