When you work with various types of posts and want to have a style for each, you can choose to use the Custom Post Type. With this you can create posts of various types, with various other elements through metaboxes. This will give a greater control over the post in itself.
Although I can customize these posts, they’ll still behave like any other post wordpress.
The advantage is that you can manage these better posts customized in front-end. It will also allow you to create "special" pages, with unique features for certain types of post.
Criação Post Type
To create a post type customized, you can use the following code:
function create_post_type_car() {
register_post_type( 'post_car', [
'labels' => [
'name' => "Cars",
'singular_name' => "Car"
],
'public' => true,
'has_archive' => true,
]
);
}
add_action( 'init', 'create_post_type_car' );
More information about the function register_post_type
, you will find in documentation.
Showing Post Type
Create a post_type is quite simple, but displayed is even simpler. For this just create a file, at the root of your theme, called single-post-car.php
. The Wordpress will automatically identify and "link" the post to that page.
This is possible thanks to Hierarchy of Templates of Wordpress.
File Hierarchy
In the database, more specifically in the table wp_posts
, the CMS will add all file information (attachments) sent and posts registered.
When the WP captures this information it follows a series of actions before displaying them, one of these actions involves fetching some files at the root of your theme and according to the mimetype
(files) or post type
(posts), search for particular file to serve as view
.
In this search (in the case of posts) the Wordpress will return the Slug (or simply Url Friendly) together with other posting information.
With this information, the WP search the file single-$posttype-$slug
, if not, it will continue searching for the necessary files until it finds or arrives in the file "father", the index.php
The order to Custom Post Types
is: single-$posttype-$Slug.php -> single-$posttype.php -> single php. -> singular php. -> index php.
Filtrando Post Type
You can also filter certain types of posts (To make a side menu, for example). For this you only need to use the class Wp_query. Ex:
$posts = new WP_Query([
"post_type" => "post_car"
]);
if ($posts->have_posts()) {
while($posts->have_posts()) {
$posts->the_post();
the_title();
/* Code Here */
}
}
Working with static pages in Wordpress
If you only have static pages (Yes, they also "descend" from single.php
), cannot use the above information as a basis. For this page type, Wordpress has another way of working.
These pages are usually used for contact, about us, calendar of events etc..
To create them is very simple, just create a php file, example aboutus.php
and use the code below to serve as view
.
Code:
<?php
/* Template Name: AboutUs */
/* Todo o restante do código aqui. Html, css, js; PHP etc */
?>
This code will make an option appear when adding a new static page.
This way you can work with several pages of templates, with different files, different information, different styles, different business logic etc.
Thanks partner. It was of huge help. Att, Mauritius.
– Maurício Biléssimo