最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Custom template not working for existing pages

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 4 years ago.

Improve this question

I was playing around with using a custom template per page. (Because I need to load different post types per page).

When creating a new page, and link my custom template to it via Page Attributes, it all works. I see on that new page the specific template applied.

Now, when selecting my specific the template on an existing page (= my production pages), it doesn't work... It just shows the content of the index.php.

I've been searching hours on this... Clearing cache etc doesn't help. Also no luck with deleting page and recreating. (Site is hosted on SiteGround)

EDIT: When adding a parent (in the page attribute box) to my page, the correct tempalte gets loaded... But why not on my root page?

EDIT2: Also my Woocommerce 'shop' page is not loading anymore, seems related, it is using index.php instead of the template of Woocommerce itself.

EDIT3: I found this: WP is not using the select custom template.php that its selected for a page -> I also indeed use custom post types with the same name as my page, that is probably the problem. But why do I have the same problem for a page 'shop', used by WooCommerce, that still uses the index.php template? I don't have a custom post type 'shop'...

Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 4 years ago.

Improve this question

I was playing around with using a custom template per page. (Because I need to load different post types per page).

When creating a new page, and link my custom template to it via Page Attributes, it all works. I see on that new page the specific template applied.

Now, when selecting my specific the template on an existing page (= my production pages), it doesn't work... It just shows the content of the index.php.

I've been searching hours on this... Clearing cache etc doesn't help. Also no luck with deleting page and recreating. (Site is hosted on SiteGround)

EDIT: When adding a parent (in the page attribute box) to my page, the correct tempalte gets loaded... But why not on my root page?

EDIT2: Also my Woocommerce 'shop' page is not loading anymore, seems related, it is using index.php instead of the template of Woocommerce itself.

EDIT3: I found this: WP is not using the select custom template.php that its selected for a page -> I also indeed use custom post types with the same name as my page, that is probably the problem. But why do I have the same problem for a page 'shop', used by WooCommerce, that still uses the index.php template? I don't have a custom post type 'shop'...

Share Improve this question edited Oct 11, 2020 at 14:45 francisMi asked Oct 10, 2020 at 13:35 francisMifrancisMi 991 bronze badge 4
  • What does it mean "It just shows the content of the index.php"? As text? – ktscript Commented Oct 10, 2020 at 17:17
  • It uses the template/php code from my index.php page in my theme folder. – francisMi Commented Oct 10, 2020 at 22:12
  • This is a difficult question to answer without seeing the interlinking code, specific functioning pieces. – Howdy_McGee Commented Oct 13, 2020 at 0:58
  • Currently having this same problem myself. Created a custom template but it only applies on new pages created. Old pages are not using this template after selecting it under "Page Attributes". – AndrewL64 Commented Jun 17, 2021 at 20:28
Add a comment  | 

1 Answer 1

Reset to default 1

as a solution, you can fully automate the selection of a template for a group of pages using slug names:

add_filter('request', function( $vars ) {

    $slug =  explode('/', explode('?', $_SERVER['REQUEST_URI'])[0]);

    array_pop ($slug);

    $temp_base = "...first_part_template_name...";
    $temp_slug = array_pop ($slug);

    if (file_exists(get_template_directory().'/'.$template.'-'.$temp_cat.'.php')) {
        get_template_part( $temp_base, $temp_slug );
    } 
}
发布评论

评论列表(0)

  1. 暂无评论