1

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.org/themes/template-files-section/page-template-files/#creating-a-custom-page-template-for-one-specific-page

Thanks

  • You want it applied by default on that page ID or you want the user to manually apply that template? – Himad May 10 at 13:23
  • I want the user can see this template page on list, only when editing the post id 115 – WolwX May 10 at 13:31
  • the blog page would not use that template anyway - it would use index.php or home.php - see developer.wordpress.org/themes/basics/template-hierarchy/… – Michael May 10 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 May 10 at 21:10
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);
| improve this answer | |
  • That's look like very good ! I did little change to call it as function, but my page id 115 don't see the template like other page, so I missed something with the condition ? Here it's my modified version => code function n1_filter_template_pageblog($post_templates, $this, $post, $post_type) { // Unless post is 115, filter your custom template from the dropdown. if($post->ID != 115){ return array_filter($post_templates, function($template_name){ return $template_name !== 'Blog'; }); } } add_filter('theme_page_templates', 'n1_filter_template_pageblog');code – WolwX May 10 at 14:34
  • if a filter uses more than the default amount of arguments(1) you have to specify the number. do it like this: add_filter('theme_page_templates', 'n1_filter_template_pageblog', 10, 4); – Himad May 10 at 14:47
  • Ok fine, it take in consideration my post id, condition is fine. But result is my post id don't show templates list now (no list, not input, nothing), other id show templates list correctly without the "Blog" template – WolwX May 10 at 14:58
  • Forgot to add the default return at the end. should work now. – Himad May 10 at 15:00
  • Thanks, now the template list is ok in post id 115, but still filtered like other post id, no presence of my template in the list – WolwX May 10 at 15:12
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.