Custom post

By 27 décembre 2015 Non classé No Comments

What are Post Types ?

Post Types is a term used to refer to different types of content in a WordPress site. In all practical sense, it should be called content type.

Post Types = Content type.

WordPress comes with five default post types : Posts are one of the post type just like pages, attachments, navigation menus, and revisions.

All of them are stored in the posts database table and are differentiated by a column called post_type.

Post types are the way to distinguish different content types in WordPress.

For example Post and Pages are both post types, but they are made to serve a different purpose (see the difference between posts and pages in WordPress).

 

What are Taxonomies?

Taxonomy in WordPress is one of those things that everyone use, but they do not know that they are using it.

WordPress taxonomies are used as a way to group posts and custom post types together.

Taxonomies = A way to group posts and custom post types together.

WordPress comes with two default Taxonomies, categories and tags.

However in some cases, you may want to expand beyond categories and tags.

WordPress allows you to create your own custom taxonomies and use them in your post types to group and sort your content.

 

How to Create Custom Post Types in WordPress

Creating a Custom Post Type – The Easy Way

The easiest way to create a custom post type in WordPress is by using a plugin. This method is recommended for beginner because it is safe and super easy.

First thing you need to do is install and activate the Custom Post Type UI plugin. Upon activation, the plugin will add a new menu item in your WordPress admin menu called CPT UI.

Now go to CPT UI » Add New to create a new custom post type.

Creating a Custom Post Type Manually

You can manually create your custom post type by adding the required code in your theme’s functions.php file or in a site-specific plugin (See: Custom Post Types Debate functions.php or Plugin).

Here is why we believe that Custom Post Types should ALWAYS live in a site-specific plugin or a separate plugin altogether.

What is a Site-Specific Plugin

Site-Specific WordPress plugin is a specific place to add snippets that you find on the web that performs functions that are not theme related.

Time to time again, you will read “how-to” articles related to WordPress all around the web (including our site) where we share snippets and suggest that you add them into your theme’s functions.php file. These snippets are not huge enough to be justified as plugins of their own, but some of them are certainly great to add specific functionality to a site that is not theme-dependent such as redirecting users to random post. Whereas other functions can be key to your specific site such as Custom Post Types.

This is when you need to add a site-specific plugin which essentially will serve as the same purpose of your theme’s functions.php file, but it will allow you to retain all these functionality when you are changing themes (see our checklist of things to do when changing WordPress themes).

Why You need a Site-Specfic Plugin for WordPress

As we mentioned above, it will allow you to retain all key additions to your site that you may have done overtime. Some functions that belong in a site specific plugin would be Custom Post Types, Adding Shortcodes, Adding WordPress Thumbnail Support, Redirecting users to a Random Post, and other independent functions that does not rely on your theme.

Above functions are something that you will need whether you change your theme or not. Can you imagine disabling a theme and then losing all of your shortocdes? That would be a total nightmare.

How to Create a Site-Specific WordPress Plugin

You may think that it may be super hard, but it really is not.

All you have to do is create a new folder in the plugins directory. Example: /wp-content/plugins/yoursitename-plugin/

Open a blank file and save it as “yoursitename-plugin.php”

Put the following code in the file:

 

Now upload this file into the folder you created in the plugins directory. Then simply activate the plugin.

From this point on, we will recommend adding the appropriate snippets rather than theme’s functions.php file.

We hope this will start a new trend that others will follow.

We have already joined the band-wagon, and we recommend that you do this as well. It is a best-practice that will save you time in the long-run.

So, back to

Creating a Custom Post Type Manually

 

 

Displaying Custom Post Types on Your Site

WordPress comes with built in support for displaying your custom post types. Once you have added a few items into your new custom post type, it is time to display them on your website.

There are a couple of methods that you can use, each one has its own benefits.

Displaying Custom Post Type Using Default Archive Template

First you can simply go to Appearance » Menus and add a custom link to your menu. This custom link is the link to your custom post type. If you are using SEO friendly permalinks then your CPT’s URL will most likely be something like this:

http://nba-pronostic.fr/equipes

If you are not using SEO friendly permalinks, then your custom post type URL will be something like this:

http://nba-pronostic.fr/?post_type=equipes

Replace example.com with your own domain name and movies with your custom post type name. Save your menu and then visit the front-end of your website. You will see the new menu you added, and when you click on it, it will display your custom post type archive page using the archive.php template file in your theme.

Using Custom Templates for CPT Archives and Single Entries

If you don’t like the appearance of the archive page for your custom post type, then you can use dedicated template for custom post type archive. To do that all you need to do is create a new file in your theme directory and name it archive-equipes.php. Replace pronostics with the name of your custom post type.

For getting started, you can copy the contents of your theme’s archive.php file into archive-equipes.php template and then start modifying it to meet your needs. Now whenever the archive page for your custom post type is accessed, this template will be used to display it.

Inside the loop :

 

Similarly, you can also create a custom template for your post type’s single entry display. To do that you need to create single-equipes.php in your theme directory. Don’t forget to replace movies with the name of your custom post type.

You can get started by copying the contents of your theme’s single.php template into single-equipes.php template and then start modifying it to meet your needs.

 

Sources :

http://www.wpbeginner.com/beginners-guide/when-do-you-need-a-custom-post-type-or-taxonomy-in-wordpress/

http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-post-types-in-wordpress/

http://www.wpbeginner.com/wp-tutorials/how-to-add-custom-meta-boxes-in-wordpress-posts-and-post-types/

http://www.wpbeginner.com/opinion/wordpress-custom-post-types-debate-functions-php-or-plugins/

http://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/

http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

https://codex.wordpress.org/Function_Reference/the_permalink

Leave a Reply