skip to Main Content

I am trying to develop a custom theme for WordPress and would really like the section to be available to select your own page template for your pages in the admin area. I notice some themes have it and others do not see images below:

Without support:

No template support

With the desired support:

Theme with template selection support

Is there some way to include this by modifying functions.php or another area of the themes files?

2

Answers


  1. In WordPress, a custom page template allows you to create different layouts or designs for specific pages on your website. This customization allows you to choose a specific template for a page in the admin area, providing flexibility in how content is displayed.

    <?php
    /*
     * Template Name: Template Name
     * */
    
    1. /* … */: This is a PHP comment block. Comments in PHP are text
      that are not executed and are used for providing explanations or
      notes within the code.
    2. Template Name: Template Name: This is a special comment used in
      WordPress to define a custom page template. By adding this comment
      block at the beginning of a PHP file within your WordPress theme,
      you’re telling WordPress that this file can be used as a custom
      template for pages. The "Template Name" is the name that will be
      displayed in the WordPress admin area when selecting a template for
      a page.
      For instance, if you have a file named custom-template.php and it contains the code you provided, when you create or edit a page in WordPress, under the Page Attributes section, you’ll see a Template dropdown where "Template Name" will appear as an option for selecting this custom template for that specific page.

    As for your goal of creating a custom theme with the ability to select page templates in the admin area, ensure the following:

    1. File Placement: Place your custom page template files within your
      WordPress theme directory.
    2. Proper Comment Block: Include the comment block Template Name:
      YourTemplate at the top of each PHP file that you want to designate
      as a custom template.
    3. WordPress Admin Area: When creating or editing a page in WordPress,
      look for the "Page Attributes" metabox. There, you should see a
      "Template" dropdown displaying the names of your custom templates.
      Select the appropriate template for the page you’re editing.
    Login or Signup to reply.
  2. Try adding support to page-attributes when registering your CPT.

    https://make.wordpress.org/support/user-manual/pages/page-attributes/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search