admin管理员组

文章数量:1407737

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;
}

本文标签: Force template on page load