• I’m building an custom post type with multiple taxonomies.

    I use https://generatewp.com/ to quickly fill in all the info I need and strip out the stuff I don’t need.

    I do the same for the custom taxonomies.

    When I generate the code with https://generatewp.com/ it gives each post type and taxonomy a function but that is not necessary because you already have the cpt function.

    How do I place the taxonomies within the cpt function?

    <?php
    if ( ! function_exists('ot_uitje_cpt') ) {
    
    	// Register Custom Post Type
    	function ot_uitje_cpt() {
    
    		$labels = array(
    			'name'                  => _x( 'Uitjes', 'Post Type General Name', 'ot_uitje' ),
    			'singular_name'         => _x( 'Uitje', 'Post Type Singular Name', 'ot_uitje' ),
    			'menu_name'             => __( 'Uitjes', 'ot_uitje' ),
    			'name_admin_bar'        => __( 'Uitje', 'ot_uitje' ),
    		);
    		$rewrite = array(
    			'slug'                  => 'uitjes',
    			'with_front'            => false,
    			'pages'                 => true,
    			'feeds'                 => true,
    		);
    		$args = array(
    			'label'                 => __( 'Uitjes', 'ot_uitje' ),
    			'labels'                => $labels,
    			'supports'              => array( 'title', 'editor', 'thumbnail' ),
    			'taxonomies'            => array( 'ot_locatie', 'ot_categorie' ),
    			'hierarchical'          => false,
    			'public'                => true,
    			'show_ui'               => true,
    			'show_in_menu'          => true,
    			'menu_position'         => 5,
    			'menu_icon'             => 'dashicons-clipboard',
    			'show_in_admin_bar'     => true,
    			'show_in_nav_menus'     => true,
    			'can_export'            => true,
    			'has_archive'           => true,		
    			'exclude_from_search'   => false,
    			'publicly_queryable'    => true,
    			'rewrite'               => $rewrite,
    			'capability_type'       => 'post',
    		);
    		register_post_type( 'ot_uitje', $args );
    
    	}
    	add_action( 'init', 'ot_uitje_cpt', 0 );
    
    }
    
    if ( ! function_exists( 'ot_uitje_locatie' ) ) {
    
    	// Register Custom Taxonomy
    	function ot_uitje_locatie() {
    
    		$labels = array(
    			'name'                       => _x( 'Locaties', 'Taxonomy General Name', 'ot_uitje' ),
    			'singular_name'              => _x( 'Locatie', 'Taxonomy Singular Name', 'ot_uitje' ),
    			'menu_name'                  => __( 'Locaties', 'ot_uitje' ),
    			'all_items'                  => __( 'Alle locaties', 'ot_uitje' ),
    		);
    		$rewrite = array(
    			'slug'                       => 'locatie',
    			'with_front'                 => false,
    			'hierarchical'               => false,
    		);
    		$args = array(
    			'labels'                     => $labels,
    			'hierarchical'               => true,
    			'public'                     => true,
    			'show_ui'                    => true,
    			'show_admin_column'          => true,
    			'show_in_nav_menus'          => true,
    			'show_tagcloud'              => true,
    			'rewrite'                    => $rewrite,
    		);
    		register_taxonomy( 'ot_locatie', array( 'ot_uitje' ), $args );
    
    	}
    	add_action( 'init', 'ot_uitje', 0 );
    
    }
    
    if ( ! function_exists( 'ot_uitje_categorie' ) ) {
    
    	// Register Custom Taxonomy
    	function ot_uitje_categorie() {
    
    		$labels = array(
    			'name'                       => _x( 'Categorie?n', 'Taxonomy General Name', 'ot_uitje' ),
    			'singular_name'              => _x( 'Categorie', 'Taxonomy Singular Name', 'ot_uitje' ),
    			'menu_name'                  => __( 'Categorie?n', 'ot_uitje' ),
    			'all_items'                  => __( 'Alle categorie?n', 'ot_uitje' ),
    		);
    		$rewrite = array(
    			'slug'                       => 'categorie?n',
    			'with_front'                 => false,
    			'hierarchical'               => false,
    		);
    		$args = array(
    			'labels'                     => $labels,
    			'hierarchical'               => true,
    			'public'                     => true,
    			'show_ui'                    => true,
    			'show_admin_column'          => true,
    			'show_in_nav_menus'          => true,
    			'show_tagcloud'              => true,
    			'rewrite'                    => $rewrite,
    		);
    		register_taxonomy( 'ot_categorie', array( 'ot_uitje' ), $args );
    
    	}
    	add_action( 'init', 'ot_uitje_categorie', 0 );
    
    }
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’m not entirely sure what you’re asking.

    There are two ways to associate taxonomies with a post type.

    One is to add them to the ‘taxonomies’ argument for the post type. The other is to add them to the object_type argument when you register the taxonomy.

    Thread Starter Unclouded Studio

    (@uncloudedstudio)

    At the moment each taxonomy has it’s own function but i don’t need it because I want to place it all under the CPT function.

    so function ot_uitje_cpt() {
    custom post type
    custom tax1
    custom tax2
    }

    instead

    function ot_uitje_cpt() {
    custom post type
    }
    function ot_uitje_locatie() {
    custom tax1
    }
    function ot_uitje_categorie() {
    custom tax2
    }

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    you need them as separate functions to correctly use the hooks.

    Thread Starter Unclouded Studio

    (@uncloudedstudio)

    Okay thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post type with multiple taxonomies’ is closed to new replies.