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

Force template on page load

programmeradmin1浏览0评论

tell me where to refer to the function to be run once at the start of the page.

Exactly I mean to set a specific template so that every time you open the page always the same template loads without regard to what template was set up in the back.

tell me where to refer to the function to be run once at the start of the page.

Exactly I mean to set a specific template so that every time you open the page always the same template loads without regard to what template was set up in the back.

Share Improve this question edited Feb 26, 2020 at 18:15 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 26, 2020 at 18:10 spokspok 72 bronze badges 1
  • If you are in controll of the template files, leaving only your index.php left should route everything to that template file. – Luckyfella Commented Feb 26, 2020 at 18:24
Add a comment  | 

1 Answer 1

Reset to default 1

This question is quite difficult to understand.

Do you mean you want to make generic template pages Programmatically? There are a few ways to do this.

If you have a page built on the backend, you can create a file within your theme folder called page-{page-name}.php. For instance, for a page called about, you should create a file called page-about.php.

If it is a post, the same logic applies, but with single-{post-name}.php.

Alternatively, if you want a template page for several pages under one file, you could use a conditional statement along with the is_page() function inside page.php (or single.php for posts).

You can also use get_post_field with a conditional, as such:

if ('contact-us' == get_post_field( 'post_name', $post->post_parent )) {
  get_template_part( 'template-parts/contact', 'page' );
  exit;
} else if ('volunteer-with-us' == get_post_field( 'post_name')) {
  get_template_part( 'template-parts/contact', 'page' );
  exit;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论