未加星标

A Quick and Dirty Guide to Post Type Templates in WordPress 4.7

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

WordPress 4.7 has delivered some fantastic new enhancements to the CMS’s template system. Templates have nowbeen expanded to include all post types, allowing developers to create more nuanced themes and at the same time allowing site owners to manage content more easily.

But how does it all work? In this article, I’ll show you how to use post type templates in your themes with a few easy examples.

So What are Templates in WordPress?

Templates are essentially special files that can change the look and feel of a page and/or add functionality to your WordPress website.

Prior to WordPress 4.7 templates were only available for pages, thus they were dubbed page templates . However, WordPress 4.7 has expanded their use to include any post type, so I am hereby renaming them “templates” for short.


A Quick and Dirty Guide to Post Type Templates in WordPress 4.7
The WordPress template selector in the Post Editor.

Once templates are registered in the theme, they can be selected on single post edit pages.

Laying the Groundwork for Using Templates

Let’s work on an example.

To test this out we should create a child theme first. Let’s use Twenty Seventeen , the new default WordPress theme.

To get started, create a new folder in your theme directory. I named mine “cppt” (short for custom post type templates) and then I added new new files: style.css and functions.php . Use the code below to set up these files:

All set! You should now see the child theme in the Appearance section of your WordPress admin. Once activated you should see a vanilla, unmodified Twenty Seventeen experience.

Creating a Custom Post Type

Let’s test the new template system with a custom post type. Imagine you’re creating a website about games, like IGN or Gamespot .

While writing about games you might publish news items, which are shorter pieces and full reviews, which provide an in-depth look at the game.

To make one look different than the other you need to use a category or some other arbitrary device; templates make this more straightforward.

I’ve omitted many labels and options and this bare bones code will be just fine for our purposes.

Creating Templates

A template is a regularphp file with a special comment at the top, which tells WordPress what the title of the template is and where it can be used.

Let’s create one for our game reviews now:

When you create a game post you should see the template selector with two options: Default and Game Review. Once you select a template for a post, the code within the template will be used for that post.


A Quick and Dirty Guide to Post Type Templates in WordPress 4.7
Here’s my default game review post.
A Quick and Dirty Guide to Post Type Templates in WordPress 4.7
And another image from my game review post.

To create the views I mostly copy-pasted from Twenty Seventeen. I created the template-game-review.php file with the following code:

I got all that from single.php in Twenty Seventeen. I modified the get_header() function with a parameter to pull in a different header (to make sure the site masthead was removed). I also modified the parameters of the get_template_part() function to make it get_template_part( 'template-parts/game/content', 'review' ); , but that’s all.

I then created the new header file header-review.php . I copy-pasted the code from Twenty Seventeen’s header.php into it and then removed the following line:

I followed this up by creating template-parts/game/content-review.php . I pasted Twenty Seventeen’s content.php into it and removed some code that wasn’t needed since it only pertained to non-single page items. I added the following code to the very top:

I added a little bit of CSS to position it to the top and give it some basic styling:

Wrapping Up

Using templates is extremely easy and provides a wealth of new options to developers. It could make our code better organized and standardized across different themes.

If you’d like to see it all in action, download thequick example I made to showcase this enhancement.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: WordCSSPHPppt
分页:12
转载请注明
本文标题:A Quick and Dirty Guide to Post Type Templates in WordPress 4.7
本站链接:http://www.codesec.net/view/523373.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(28)