{"id":312,"date":"2018-06-17T02:26:53","date_gmt":"2018-06-16T18:26:53","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=312"},"modified":"2018-06-17T02:27:20","modified_gmt":"2018-06-16T18:27:20","slug":"how-to-create-a-custom-page-template-in-wordpress","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-create-a-custom-page-template-in-wordpress\/","title":{"rendered":"How to create a custom page template in WordPress"},"content":{"rendered":"<p>We use <a href=\"https:\/\/codex.wordpress.org\/Pages\" rel=\"noopener\" target=\"_blank\">pages<\/a> in <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> to hold content of our website that are not specifically time-dependent. <\/p>\n<p>Examples of such content at <a href=\"https:\/\/www.techcoil.com\">Techcoil<\/a> include the <a href=\"https:\/\/www.techcoil.com\/about\">about page<\/a>, <a href=\"https:\/\/www.techcoil.com\/disclaimer\" rel=\"noopener\" target=\"_blank\">disclaimer page<\/a> and <a href=\"https:\/\/www.techcoil.com\/raspbian-stretch-lite-project-ideas\/\" rel=\"noopener\" target=\"_blank\">Raspbian Stretch Lite project ideas<\/a>. <\/p>\n<p>When you want to show content in different page layouts, you can create different customized page templates to show content in different ways.<\/p>\n<p>In case you need a reference, this post describes how to create a custom page template in WordPress.<\/p>\n<h2>Where to place a custom page template file in your WordPress installation<\/h2>\n<p>Since WordPress page templates belong to a WordPress theme, we will need to put them inside a theme folder. As an illustration, if your WordPress installation is in <code>\/var\/www\/wp_site<\/code> and your theme folder is named as <code>my_theme<\/code>, you will place a <code>.php<\/code> file at <code>\/var\/www\/wp_site\/wp-content\/themes\/my_theme<\/code> folder.<\/p>\n<h2>How to name the custom WordPress page template file<\/h2>\n<p>You can name your WordPress page template file in any way that the host operating system allows. However, the file has to end with a <code>.php<\/code> extension. <\/p>\n<p>For example, if we want to create a page template for displaying content without sidebars, we can name it <code>page_without_sidebars.php<\/code>.<\/p>\n<h2>How to tell WordPress that the current file is a custom WordPress page template<\/h2>\n<p>In order to tell WordPress that the current file is a page template, include the following comments inside the template file:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/*\r\nTemplate Name: Page without sidebar\r\n*\/\r\n?&gt;\r\n<\/pre>\n<p>Given that comment segment, WordPress will display the page template name as an option under the <strong>Page Attribute<\/strong> section:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/wordpress-page-attribute-section-with-template-without-sidebar-as-an-option.gif\" alt=\"WordPress page attribute section with template without sidebar as an option\" \/><\/p>\n<p>However, note that each custom WordPress page template in your theme folder should have a unique name. When you introduce a new template with a duplicate name, WordPress will ignore that new template file.<\/p>\n<h2>How to show the page content and title from the custom WordPress page template file<\/h2>\n<p>Once a <code>.php<\/code> file is marked as a template, the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\" rel=\"noopener\" target=\"_blank\">WordPress functions<\/a> that are available to pages can be used. As an illustration, the following is a simplified template file that shows the page title and content:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/*\r\nTemplate Name: Page without sidebar\r\n*\/\r\n\r\n\/\/ Show the contents from header.php\r\nget_header();\r\n\r\n\/\/ Start the loop.\r\nwhile ( have_posts() ) : the_post();\r\n\r\n  \/\/ Show the page title\r\n  the_title( '&lt;h1&gt;', '&lt;\/h1&gt;' );\r\n\r\n  \/\/ Show the page content\r\n  the_content();\r\n\r\nendwhile;\r\n\r\n\/\/ Show the contents from footer.php\r\nget_footer();\r\n?&gt;\r\n<\/pre>\n<p>Whenever a page is given this template, its content and title will be shown in between the header and footer content. <\/p>\n<p>In case you need to show more details of the page you can check out <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\" rel=\"noopener\" target=\"_blank\">WordPress function reference<\/a>. In addition, you can add HTML elements to the template if you need to render different visual elements. <\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-52\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-52&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-52&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F312&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>We use <a href=\"https:\/\/codex.wordpress.org\/Pages\" rel=\"noopener\" target=\"_blank\">pages<\/a> in <a href=\"https:\/\/wordpress.org\/\" rel=\"noopener\" target=\"_blank\">WordPress<\/a> to hold content of our website that are not specifically time-dependent. <\/p>\n<p>Examples of such content at <a href=\"https:\/\/www.techcoil.com\">Techcoil<\/a> include the <a href=\"https:\/\/www.techcoil.com\/about\">about page<\/a>, <a href=\"https:\/\/www.techcoil.com\/disclaimer\" rel=\"noopener\" target=\"_blank\">disclaimer page<\/a> and <a href=\"https:\/\/www.techcoil.com\/raspbian-stretch-lite-project-ideas\/\" rel=\"noopener\" target=\"_blank\">Raspbian Stretch Lite project ideas<\/a>. <\/p>\n<p>When you want to show content in different page layouts, you can create different customized page templates to show content in different ways.<\/p>\n<p>In case you need a reference, this post describes how to create a custom page template in WordPress.<\/p>\n","protected":false},"author":1,"featured_media":1030,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375,4],"tags":[16,13,5,170],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/wordpress-page-attribute-section-with-template-without-sidebar-as-an-option.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-52","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/312"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=312"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/312\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1030"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}