Force a specific template as default

Question

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.

0
, , kikaweb 4 years 2020-02-24T08:38:24-05:00 0 Answers 58 views 0

Leave an answer

Browse
Browse