# Setting blade template programmatically by post-type

**URL:** https://discourse.roots.io/t/setting-blade-template-programmatically-by-post-type/9038
**Category:** sage
**Tags:** sage9, blade
**Created:** 2017-03-09T14:29:06Z
**Posts:** 7

## Post 1 by @Daniel_Willitzer — 2017-03-09T14:29:06Z

Hey there,

I have been working on setting a particular template by default for a custom post-type, the typical way I would set it is not leading to much success, having mixed results with the snippet below. If the page is ‘business\_page,’ then it should pass the if condition with the correct template.

With Sage 9 any suggestions, and what I could be doing better?  
Really appreciate any advice.

```
add_filter( 'page_template', 'cpt_template_set' );
function cpt_template_set( $page_template ) {
	global $post;
	$page_id = $post->ID;
	
	if ( get_post_type($page_id) == 'business_page') {
		$page_template = get_template_directory() . '/template-page-business.blade.php';
	}
	//var_dump( $page_id);
	//var_dump( $page_template);

	return $page_template;
}
```

---

## Post 2 by @kaisermann — 2017-03-09T21:30:37Z

If I understood you correctly, you can do this by using the `single-{CustomPostType}.blade.php` file.

In your case, it would be something like `single-business_page.blade.php`

---

## Post 3 by @kalenjohnson — 2017-03-10T02:01:07Z

I agree with kaisermann. Is there a reason you’re trying to change the page template via code when you can just set up a template for the custom post type, @Daniel_Willitzer ?

---

## Post 4 by @Daniel_Willitzer — 2017-03-13T15:02:43Z

@kaisermann, thank you for the reminder! Works as expected.

@kalenjohnson & @kaisermann, reason, there are multiple layouts for “business pages” that differ in style and functionality.

---

## Post 5 by @jamesfacts — 2017-03-13T20:32:51Z

I’m in a similar situation—I have a single custom post type with it’s own taxonomy, and layouts that vary based on the taxonomy in question.

I could pull layout elements in conditionally, but it seems a little cleaner to use individual template files. Am I barking up the wrong tree?

---

## Post 6 by @weizung — 2018-07-20T13:33:30Z

I try to duplicate the `single.blade.php` and rename to `single-portfolio.blade.php` which it gave me the 404 page, said `Page not found`. Do I need to add any filters or actions to register it with SAGE 9???

thanks!

---

## Post 7 by @weizung — 2018-07-20T13:39:27Z

**Fixed by re-saving the permalink on the WP dashboard.**

> [@Template single Custom Post Type - get 404](https://discourse.roots.io/t/template-single-custom-post-type-get-404/6918/2):
>
> Did you try [https://codex.wordpress.org/Function\_Reference/flush\_rewrite\_rules](https://codex.wordpress.org/Function_Reference/flush_rewrite_rules)? Re-saving your permalink options at /wp-admin/options-permalink.php should also do it.
