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
| Filter | What it changes | Example condition |
|---|---|---|
generate_sidebar_layout | Sidebar placement/visibility | is_page( 'landing' ) |
generate_content_layout (example) | Content container style | is_single() |
body_class | CSS targeting | is_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.
flowchart LR
REQ[Request] --> COND[Conditional check]
COND --> RET[Return layout value]
RET --> OUT[Layout output]
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 type | Desired layout | Implementation |
|---|---|---|
| Landing pages | No sidebar | Filter + specific page slug |
| Blog posts | Right sidebar | Default or filter |
| Checkout | No header/footer | Layout 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
| Practice | Why |
|---|---|
| Keep conditions narrow | Prevents unexpected layouts |
| Prefer one rule per intent | Easier debugging |
| Document return values | Avoid magic strings |
| Test logged-out | Caching differences |
| Keep a rule inventory | Prevents overlap |
| Prefer IDs for critical pages | Slugs can change |
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Filter has no effect | Wrong filter name | Confirm with grep in the parent theme |
| Layout changes randomly | Overlapping rules | Consolidate conditions and document |
| Changes not visible | Cache | Purge LSCache |
Hands-On
- Find two layout-related filters in GeneratePress.
- Implement one condition (no sidebar on one page).
- Add a body class and write a small CSS rule for that class.
- Add one more condition (product/shop or archive) and confirm no overlap.
- 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
- Next: Accessibility & Semantics
- Related: Global Layout Framework