Skip to content

Instantly share code, notes, and snippets.

@larodiel
Created April 2, 2022 05:48
Show Gist options
  • Save larodiel/f5061c1e84b551547dcdc268b8f222b4 to your computer and use it in GitHub Desktop.
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)
<?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