Creating a single-{category}.html template with query block filter
-
I was using this function:
/**- Assign a block template based on post category
*/
function assign_custom_block_template( $post_id, $post ) {
// Check if it’s the correct post type
if ( ‘post’ !== $post->post_type ) {
return;
} // Define category-to-template mappings
$categories_to_templates = [
‘motherhood’ => ‘single-motherhood’,
‘business’ => ‘single-business’, // Assuming you have a ‘single-business’ template
‘lifestyle’ => ‘single-lifestyle’ // Assuming you have a ‘single-lifestyle’ template
]; foreach ( $categories_to_templates as $category => $template_slug ) {
if ( has_category( $category, $post_id ) ) {
$block_templates = get_block_templates( array( ‘slug’ => $template_slug ), ‘wp_template’ );
if ( !empty( $block_templates ) ) {
$custom_template = reset( $block_templates );
update_post_meta( $post_id, ‘_wp_page_template’, $custom_template->slug );
break; // Stop the loop after finding and setting a template
}
}
}
}
add_action( ‘save_post’, NAMESPACE . ‘\assign_custom_block_template’, 99, 2 );
To automatically assign a template to a category when selected. It works when I do this, but when I try to update swap templates from let’s say lifestyle to business it updates on the backend but on the front end it still uses the single-lifestyle.html template. I did some debugging and found this:
[30-Jun-2024 14:08:37 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
[30-Jun-2024 14:08:37 UTC] Intended Template:
[30-Jun-2024 14:08:37 UTC] Template Saved: wp-custom-template-single-business
[30-Jun-2024 14:10:57 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
[30-Jun-2024 14:10:57 UTC] Intended Template:
[30-Jun-2024 14:10:57 UTC] Template Saved: wp-custom-template-single-lifestyle
[30-Jun-2024 14:10:57 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 241
[30-Jun-2024 14:10:57 UTC] Intended Template:
[30-Jun-2024 14:10:57 UTC] Template Saved: wp-custom-template-single-business
[30-Jun-2024 14:16:01 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:16:01 UTC] Intended Template:
[30-Jun-2024 14:16:01 UTC] Template Saved: wp-custom-template-single-business
[30-Jun-2024 14:16:02 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:16:02 UTC] Intended Template:
[30-Jun-2024 14:16:02 UTC] Template Saved: wp-custom-template-single-lifestyle
[30-Jun-2024 14:16:09 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:16:09 UTC] Intended Template:
[30-Jun-2024 14:16:09 UTC] Template Saved: wp-custom-template-single-lifestyle
[30-Jun-2024 14:16:09 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:16:09 UTC] Intended Template:
[30-Jun-2024 14:16:09 UTC] Template Saved: wp-custom-template-single-business
[30-Jun-2024 14:21:12 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:21:12 UTC] Intended Template:
[30-Jun-2024 14:21:12 UTC] Template Saved: wp-custom-template-single-lifestyle
[30-Jun-2024 14:21:13 UTC] PHP Warning: Undefined array key “_wp_page_template” in /Users/xxxxx/Local Sites/four-black-mompreneurs/app/public/wp-content/themes/ollie/functions.php on line 245
[30-Jun-2024 14:21:13 UTC] Intended Template:I got frustrated and just did it manually. Judging by my code where am I going wrong? thanks
- Assign a block template based on post category
- The topic ‘Creating a single-{category}.html template with query block filter’ is closed to new replies.