0

[ EDIT: I'm using Chaplin theme as the parent theme ]

I'm working on a child theme and I need to set a specific template as default for all posts (there's no custom post types, just regular posts).

Just to be clear: I need that every time a user adds a new post, "My custom template" is picked instead of the "Default template".

I asked the developer of the original theme for a suggestion and he pointed me to this answer. Then I edited the code to adapt it to my case (as I'm working on posts, not pages).

However, this code doesn't work for me, the Default template is still default.
This is the code I'm using:

    add_action('add_meta_boxes', 'my_default_page_template', 1);
function my_default_page_template() {

    global $post;
    if ( 'post' == $post->post_type 
        && 0 != count( get_page_templates( $post ) ) 
        && get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
        && '' == $post->page_template // Only when page_template is not set
    ) {
        $post->page_template = "my-custom-template.php";
    }

}

I'm sure I'm missing something, but I'm no expert in coding and need someone to point me to the right direction (and possibly explaining me what I'm doing wrong so that I can learn something).

Thanks in advance.

  • Do you still need the original template? Why not just replace the default template with your template in the child theme? – Jacob Peattie Feb 24 at 13:29
1

Instead of setting yourtemplate.php as the default, why don't you change the default template, and put the original default in yourtemplate.php? AKA swap them?

Otherwise you'll have to go back through all your posts and change them, and, mess around with complicated filters that might not work 100%

|improve this answer|||||
  • Thanks Tom, but this is not a solution in this case because of the way the parent theme was created. I didn't create the parent theme, so I have to stick with the original. If I could choose I'd certainly go this route (that's actually what I usually do). – kikaweb Feb 24 at 17:28
  • 1
    You don't need to modify the parent theme to do this, but, you should explain in your question that you have unusual circumstances, including which theme you're using. I cannot imagine a scenario that would stop this from working, but, there is possibility that this problem can only be solved via the theme vendors support, or undoing what that theme is doing. Without knowing it's difficult to answer – Tom J Nowell Feb 24 at 20:28
  • I wrote that I'm working on a child theme and I'm not trying to modify the parent. However, I'll edit the question to add the name of the parent theme (it's Chaplin by the way). Sorry if my comment wasn't clear, English is not my native language. – kikaweb Feb 25 at 10:31

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.