How to Create Custom Page Template in WordPress?

By default, WordPress pages use the theme’s page template, and blog posts use the single post template. This works well for most sites, but sometimes you need more control. We need a dedicated portfolio page, a custom landing page, or a page that displays posts from a custom post type. Custom page templates let you define your own layout and content for any page, while still using your theme’s header and footer.

Before We Start Coding

  • Always work in a child theme or use a plugin like Code Snippets for PHP changes.
  • Make sure you already have a custom post type called portfolio.

Ensure that a portfolio Custom post type is already registered. For reference, see our detailed guide: How to Create a Custom Post Type in WordPress.

Step 1: Create the Template File

Inside your child theme folder, create a file called: template-portfolio.php

Paste this code inside it:

<?php
/**
 * Template Name: Custom Template
 * Description: A simple template to display Portfolio items only.
 */

get_header(); // Include header
?>

<div class="portfolio-page">
  <div class="container">

    <h1><?php the_title(); ?></h1>

    <div class="page-content">
      <?php while (have_posts()) : the_post(); the_content(); endwhile; ?>
    </div>

    <?php
    // Custom query for Portfolio posts
    $portfolio_query = new WP_Query(array(
      'post_type'      => 'portfolio',
      'posts_per_page' => 10, // Number of portfolio items to show
    ));

    if ($portfolio_query->have_posts()) :
    ?>
      <section class="portfolio-section">
        <h2>Latest Portfolio Projects</h2>
        <div class="portfolio-grid">
          <?php while ($portfolio_query->have_posts()) : $portfolio_query->the_post(); ?>
            <div class="portfolio-item">
              <?php if (has_post_thumbnail()) : ?>
                <a href="<?php the_permalink(); ?>">
                  <?php the_post_thumbnail('medium'); ?>
                </a>
              <?php endif; ?>
              <h3><?php the_title(); ?></h3>
              <div><?php the_excerpt(); ?></div>
            </div>
          <?php endwhile; wp_reset_postdata(); ?>
        </div>
      </section>
    <?php else: ?>
      <p>No portfolio items found.</p>
    <?php endif; ?>

  </div>
</div>

<?php get_footer(); ?>

Step 2: Assign the Template to a Page

  1. In your dashboard, go to Pages → Add New.
  2. Give it a title like “Our Work” or “Portfolio.”
  3. In the right-hand “Template” dropdown, choose Custom Template.
  4. Publish the page.

Now visit the page on the front end and you’ll see your page title and intro text, followed by a grid of portfolio items.