PHP DevelopmentWordpress Development

Creating Single Post & Page Template in WordPress theme file with code

By default when the single post doesn't have template, WordPress will use the index.php page format for single posts.
Create-custom-single-post-templates-in-wordpress-og

In WordPress, without a custom single post template (single.php), individual posts use the default template defined by the theme. A custom single post template allows you to control the layout and styling of single post pages, offering flexibility in design and functionality. You can create a single post template by adding a single.php file to your theme folder, which WordPress will automatically recognize and use for displaying single post pages, overriding the default template.

In short, By default when the single post doesn’t have template, WordPress will use the index.php page format for single posts.

Creating Single Pos template

In order to create single post page template you can follow this steps.

  1. Inside your custom theme folder, create a new file named single.php with exact name in the location where index.php is located or it is inside “wp-content/themes/your-custom-theme“.
  2. Customize the single.php file to define the layout and styling of single post pages.
  3. Use WordPress template tags such as the_title(), the_content(), the_author() and other predefined functions by wordpress to display post content dynamically.
  4. Optionally, add additional HTML, CSS, and PHP code to further customize the appearance and functionality of single post pages.
  5. Save the single.php file, and WordPress will automatically use it to display single post pages on your website, overriding the default template.

But remember, the name should be exactly as single.php otherwise WordPress will not display your single post template

Creating WordPress Single Page Template

Single post pages in WordPress are individual pages dedicated to showcasing a single post’s content. They typically include the post’s title, content, metadata, and any additional information associated with the post. WordPress automatically generates these pages for each post you create, providing a structured layout for displaying content.

Single pages, on the other hand, are standalone pages in WordPress that contain unique content separate from posts. They are often used for static content like about pages, contact pages, or landing pages. Single pages are created and managed through the WordPress dashboard’s Pages section, where you can customize their content and appearance using the built-in editor or page builder plugins.

Creating single page template inside WordPress theme folder is simple and straight forward like creating single post template The only difference is that you have to rename the file name as page.php exactly

  1. Inside your custom theme folder, create a new file named page.php with exact name in the location where index.php is located or it is inside “wp-content/themes/your-custom-theme“.
  2. Customize the page.php file to define the layout and styling of single post pages.
  3. Use WordPress template tags such as the_title(), the_content() and other predefined functions by WordPress to display post content dynamically like we did for single post.

Adding Page title and post title to browser tab

Post Title screenshot
Post Title screenshot

Page titles in WordPress works a little bit differently compared to HTML templates. In HTML file you could add different pages and in head part of the page you pass the title of the page. You could do it in WordPress too (add page title in header.php file), but that will result you in strictly the same title in all pages and posts.

In WordPress there is a function that is called add_theme_support(); in this function you can pass value of title-tag to get pages and posts title dynamically.

Steps to add title to pages and posts

  1. Create a new custom function with your function name in functions.php file & inside the function call the wp function called add_theme_support('title-tag'); with title tag value.
  2. Call add action hook with the values when value from wp & your custom function name
function tiomate_title_tag(){
    add_theme_support('title-tag');
}
add_action('after_setup_theme', 'tiomate_title_tag');

Final Tip, Adding links in WordPress for pages & posts

In wordpress, there is a function called site_url(); which is used to get the website home page link. While adding links to your posts & pages, site url function site_url(); which prints home page link, can be used as <a href="<?php echo site_url('/about-us-page'); ?>" >About Us</a>

—- Thankyou for reading —-

Shares:

Related Posts

3.5m Balegojo Cafe Table Umbrella - Green Inner Part

Aaa Hello

Morning Henoka, this site https://immigrantsgateway.ca/wp-admin is showing error when I tried to edit some words as Beki sent me. Maybe it sent you email instructions on admin emailMorning Henoka, this

Top Categories

PHP Development
22
WordPress Theme Development
21
Wordpress Development
18
WordPress JS Development
13
Show Comments (0)
Leave a Reply

Your email address will not be published. Required fields are marked *