Created
April 2, 2022 05:48
-
-
Save larodiel/f5061c1e84b551547dcdc268b8f222b4 to your computer and use it in GitHub Desktop.
Load a single post page based on the category (can be adapted to taxonomy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Load Single Page by Category | |
*/ | |
add_filter('single_template', function ($template) { | |
foreach ((array) get_the_category() as $cat) { | |
$templateFile = STYLESHEETPATH . "/single-category-{$cat->slug}.php"; | |
if (file_exists($templateFile)) { | |
return $templateFile; | |
} | |
if ($cat->parent) { | |
$cat = get_the_category_by_ID($cat->parent); | |
$templateFile = STYLESHEETPATH . "/single-category-{$cat->slug}.php"; | |
if (file_exists($templateFile)) { | |
return $templateFile; | |
} | |
} | |
} | |
return $template; | |
}); | |
/** | |
* Sage 9 Version | |
**/ | |
add_filter('single_template', function ($template) { | |
foreach ((array) get_the_category() as $cat) { | |
$templateFile = STYLESHEETPATH . "/views/single-category-{$cat->slug}.blade.php"; | |
if (file_exists($templateFile)) { | |
return $templateFile; | |
} | |
if ($cat->parent) { | |
$cat = get_the_category_by_ID($cat->parent); | |
$templateFile = STYLESHEETPATH . "/views/single-category-{$cat->slug}.blade.php"; | |
if (file_exists($templateFile)) { | |
return $templateFile; | |
} | |
} | |
} | |
return $template; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment