In WordPress, post and page represent contents for website. Taxonomy are categories for posts. You can create your won custom post type with plugin.
Some codes which we will use :
- register_post_type
- register_taxonomy
- add_filter, single_template, taxonomy_template
Code to create custom post type
function wporg_custom_post_type()
{
register_post_type('wporg_product',
array(
'labels' => array(
'name' => __('Products'),
'singular_name' => __('Product'),
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ), // my custom slug
)
);
}
add_action('init', 'wporg_custom_post_type');
Code to create taxonomy
function wporg_register_taxonomy_course()
{
$labels = [
'name' => _x('Courses', 'taxonomy general name'),
'singular_name' => _x('Course', 'taxonomy singular name'),
'search_items' => __('Search Courses'),
'all_items' => __('All Courses'),
'parent_item' => __('Parent Course'),
'parent_item_colon' => __('Parent Course:'),
'edit_item' => __('Edit Course'),
'update_item' => __('Update Course'),
'add_new_item' => __('Add New Course'),
'new_item_name' => __('New Course Name'),
'menu_name' => __('Course'),
];
$args = [
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => ['slug' => 'course'],
];
register_taxonomy('course', ['wporg_product'], $args);
}
add_action('init', 'wporg_register_taxonomy_course');
if you want to use template in plugin folder :
add_filter('single_template','your_single_template_function');
add_filter( 'taxonomy_template','your_taxonomy_template_function');
Here is the full code and don’t forget to change permalink after you activate the plugin, otherwise it will show 404 page :
<?php
/*
Plugin Name: ZZ Custom Post
Plugin URI: http://www.wpamanuke.com
Description: Sample Custom Post
Version: 1.0.1
Author: WPAmaNuke
Author URI: https://wpamanuke.com
*/
define( 'ZZ_PRODUCT_DIR' , plugin_dir_path( __FILE__ ) );
function wporg_custom_post_type()
{
register_post_type('wporg_product',
array(
'labels' => array(
'name' => __('Products'),
'singular_name' => __('Product'),
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'products' ), // my custom slug
)
);
}
add_action('init', 'wporg_custom_post_type');
/* Filter the single_template with our custom function*/
add_filter('single_template', 'my_custom_template');
function my_custom_template($single) {
global $post;
/* Checks for single template by post type */
if ( $post->post_type == 'wporg_product' ) {
if ( file_exists( ZZ_PRODUCT_DIR . '/single.php' ) ) {
return ZZ_PRODUCT_DIR . '/single.php';
}
}
return $single;
}
function wporg_register_taxonomy_course()
{
$labels = [
'name' => _x('Courses', 'taxonomy general name'),
'singular_name' => _x('Course', 'taxonomy singular name'),
'search_items' => __('Search Courses'),
'all_items' => __('All Courses'),
'parent_item' => __('Parent Course'),
'parent_item_colon' => __('Parent Course:'),
'edit_item' => __('Edit Course'),
'update_item' => __('Update Course'),
'add_new_item' => __('Add New Course'),
'new_item_name' => __('New Course Name'),
'menu_name' => __('Course'),
];
$args = [
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => ['slug' => 'course'],
];
register_taxonomy('course', ['wporg_product'], $args);
}
add_action('init', 'wporg_register_taxonomy_course');
function my_custom_archive( $template ) {
$mytemplate = ZZ_PRODUCT_DIR . '/archive.php';
if( is_tax( 'course') && is_readable( $mytemplate ) )
$template = $mytemplate;
return $template;
}
add_filter( 'taxonomy_template', 'my_custom_archive');
single.php
<?php
$args = [
'post_type' => 'wporg_product',
'posts_per_page' => 10,
];
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="entry-content">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php
}
archive.php
<?php
$args = [
'post_type' => 'wporg_product',
'posts_per_page' => 10,
];
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
?>
<div class="entry-content">
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a>
<?php the_content(); ?>
</div>
<?php
}