Force a specific template as default

[ 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.

Topic page-attributes templates posts Wordpress

Category Web


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%

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.