Hacking Book | Free Online Hacking Learning


wordpress custom post types (part 2): create individual content types and taxonomies

Posted by agaran at 2020-04-10

In the first part of the Custom Post type article I explained the practical WordPress option to create your own content types in theory. Today, however, it goes into practice and I describe to you how to equip your WordPress theme with Custom Post types and Custom Taxonomies itself and thus WordPress to a strong content management System can be made.

1. Create Custom Post Types in Theme

In the first part about the Custom Post Types in WordPress I already introduced the helpful plugin Custom Post Type UI to create new Custom Post Types and Taxonomies.

Of course, you can also skip a plugin and insert the required code yourself into your theme files.

1.1. Create Custom Post Types via the functions.php

For example, to convert the Custom Post Type "Projects" for an individual portfolio element into WordPress, you open the functions.php file of your theme and insert the following code:

In the code, the WordPress function register/ post/ type is used to define the new content type. Under Supports you can specify which fields you need to write your individual content (in this case the projects).

A detailed description of the possible arguments for register/ post type can be found in the WordPress Codex. Also on Justin Tadlock's blog in the article "Custom Post Types in WordPress" you can learn more about the function and the additional options.

The new Projects section is now integrated into your admin menu. To create a new project, you can simply click on Projects / Create.

Now you can create a new project for your portfolio.

1. 2. Create custom taxonomies for the new content type

In order to create your own values (called taxonomies, these are to be compared with the keywords and categories) for your projects in the admin area, you can create your own custom taxonomies again using the code in Functions.php.

For example, you can specify on your project pages which techniques (skills) were used in the projects. To do this, you use the taxonomy "Skills" to enter values such as HTML, CSS, WordPress, Photoshop or Illustration.

To do this, you will expand your code in the functions.php file by the paragraph register/ taxonomy:

In addition to entering new skills within the meta-box during writing, you can now manage and customize your Custom Taxonomies skills using the Admin Project/ Skills menu.

So the Skills work just like the keywords and categories on WordPress pages and articles.

For more information and a detailed explanation of the Custom Taxonomies, please see the article "Introducing WordPress 3 Custom Taxonomies" on the Nettuts blog.

1.3. An individual template for the new content type

For example, you can remove the sidebar (get sidebar) to present your projects across the entire website width. You probably do not need the comment function for your projects either.

1.4. Publish Custom Taxonomies in Template

To share your newly created Custom Taxonomies (here in the example the Skills) in the single-projects.php file, you need the get the term u list function.

The code to share the Skills for your project is:

More information about get the term u list can also be found in the WordPress Codex.

You can also implement further layout adjustments using HTML and corresponding CSS styles.

2. Create Custom Post Types and Taxonomies with the Custom Post Type UI plugin

If the method described contains a little too many code fragments, you can also use one of the helpful plugins for creating Custom Post Types and Custom Taxonomies.

For the Custom Post Type UI plugin, the plugin author Brad Williams has prepared a practical, small video tutorial for the use of the plugin:

Custom Post Type UI Screencast by Brad Williams on Vimeo.

Three. Custom Post Types and Permalinks

If you only see a 404-error message when calling your new content, you may need to re-save the permalink settings in the WordPress admin.

If this does not help, you can add the flush-u-rewrite-u-rules function to your functions.php file. However, you should only do this when enabled or disabled. More information about Custom Posts Types and Permalinks can also be found in the article "Custom Post Types and Permalinks" on WPEngineer and on Frank Bunges blog under "WordPress Custom Post Type and Permalinks".

You can also use the Simple Custom Post Type Archives plugin to create an archive, e.g. under deindomain.de/projects/ which unfortunately is not the case to date by default.

Four. More reading material

Do you have any questions or other tips on using the Custom Post Types in WordPress or do you know tutorials or interesting blog articles dealing with the implementation of individual content in WordPress? I am very pleased with your feedback and tips!