I wish to limit the possibility to use a specific template page only for post id x
I have created a page-blog.php template and I want it usable only for the post id 115 (id of my blog page).
Actually, in administration, all posts have the template on the models list native metabox, so perhaps we can limit or filter this models list ?
I checked this wordpress page but impossible to achieve my goal by using the slug method =>
Thanks
I wish to limit the possibility to use a specific template page only for post id x
I have created a page-blog.php template and I want it usable only for the post id 115 (id of my blog page).
Actually, in administration, all posts have the template on the models list native metabox, so perhaps we can limit or filter this models list ?
I checked this wordpress page but impossible to achieve my goal by using the slug method => https://developer.wordpress/themes/template-files-section/page-template-files/#creating-a-custom-page-template-for-one-specific-page
Thanks
Share Improve this question asked May 10, 2020 at 13:03 WolwXWolwX 112 bronze badges 4- You want it applied by default on that page ID or you want the user to manually apply that template? – Himad Commented May 10, 2020 at 13:23
- I want the user can see this template page on list, only when editing the post id 115 – WolwX Commented May 10, 2020 at 13:31
- the blog page would not use that template anyway - it would use index.php or home.php - see developer.wordpress/themes/basics/template-hierarchy/… – Michael Commented May 10, 2020 at 20:16
- Only if you specified the blog page in reading settings, in my case I don't specified any page as blog page. As as answered to myself, I have found the correct code solution, thanks – WolwX Commented May 10, 2020 at 21:10
2 Answers
Reset to default 1/**
* Filters list of page templates for a theme.
* @param string[] $post_templates Array of template header names keyed by the template file name.
* @param WP_Theme $this The theme object.
* @param WP_Post|null $post The post being edited, provided for context, or null.
* @param string $post_type Post type to get the templates for.
*/
add_filter('theme_templates', function($post_templates, $this, $post, $post_type){
// Unless post is 115, filter your custom template from the dropdown.
if(!empty($post) && $post->ID != '150'){
return array_filter($post_templates, function($template_name){
return $template_name !== 'your_template_name';
});
}
return $post_templates;
}, 20, 4);
I finally found a way to do it, based on the fact templates are stored as array (as Himad show me), we can filter this array and remove my template_name with array_diff php function =>
function n1_filter_template_pageblog($post_templates, $this, $post, $post_type) {
$template_tofiltrate = 'Blog';
$postid_exception = '115';
if(get_the_ID() != $postid_exception){
return array_diff($post_templates, array($template_tofiltrate));
}
return $post_templates;
}
add_filter('theme_page_templates', 'n1_filter_template_pageblog', 10, 4);
This one worked fine for me, all page won't see the template named "Blog" excepted the page with postid 115.