dogcpt_tax.php

Dog CPT and Taxonomy Plugin

This plugin provides a straightforward way to manage dog listings on your WordPress site. It introduces a custom post type 'Dog' for adding and managing individual dog listings, and a custom taxonomy 'Dog Breeds' for organizing these listings by breed. It's equipped with an auto-populate feature that fills the taxonomy with demo data if it's empty.

<?php /* Plugin Name: Dog CPT and Taxonomy Description: This plugin creates a custom post type 'Dog' and a custom taxonomy 'Dog Breeds'. Version: 1.0 Author: CodeWP Text Domain: codewp */ // Register Custom Post Type function create_dog_cpt() { $labels = array( 'name' => _x( 'Dogs', 'Post Type General Name', 'codewp' ), 'singular_name' => _x( 'Dog', 'Post Type Singular Name', 'codewp' ), ); $args = array( 'label' => __( 'Dog', 'codewp' ), 'labels' => $labels, 'supports' => array('title', 'editor'), 'public' => true, 'has_archive' => true, ); register_post_type( 'dog', $args ); } add_action( 'init', 'create_dog_cpt', 0 ); // Register Custom Taxonomy function create_dog_breeds_tax() { $labels = array( 'name' => _x( 'Dog Breeds', 'Taxonomy General Name', 'codewp' ), 'singular_name' => _x( 'Dog Breed', 'Taxonomy Singular Name', 'codewp' ), ); $args = array( 'labels' => $labels, 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'show_in_rest' => true, ); register_taxonomy( 'dog_breeds', array('dog'), $args ); } add_action( 'init', 'create_dog_breeds_tax', 0 ); // Populate the taxonomy with 200 demo records if empty function populate_dog_breeds() { $terms = get_terms( array( 'taxonomy' => 'dog_breeds', 'hide_empty' => false, ) ); if(empty($terms)) { for($i=1; $i<=200; $i++) { wp_insert_term( 'Breed ' . $i, // the term 'dog_breeds' // the taxonomy ); } } } add_action( 'init', 'populate_dog_breeds' );

Frequently Asked Questions

Custom post types are content types like posts and pages that you can create to suit your content needs.