WordPress.org

Forums

What is the problem in my custom post type code (3 posts)

  1. mannu4u4u
    Member
    Posted 1 year ago #

    I am creating a custom post type named product and using the code below:

    function my_custom_post_product() {
    	$labels = array(
    		'name'               => _x( 'Products', 'post type general name' ),
    		'singular_name'      => _x( 'Product', 'post type singular name' ),
    		'add_new'            => _x( 'Add New', 'book' ),
    		'add_new_item'       => __( 'Add New Product' ),
    		'edit_item'          => __( 'Edit Product' ),
    		'new_item'           => __( 'New Product' ),
    		'all_items'          => __( 'All Products' ),
    		'view_item'          => __( 'View Product' ),
    		'search_items'       => __( 'Search Products' ),
    		'not_found'          => __( 'No products found' ),
    		'not_found_in_trash' => __( 'No products found in the Trash' ),
    		'parent_item_colon'  => '',
    		'menu_name'          => 'Products'
    	);
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'menu_icon' => get_stylesheet_directory_uri() . '/images/admin/wp-logo-small.png',
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => null,
    		//'supports' => array('title','editor','author','thumbnail','post-thumbnails','excerpts','trackbacks','custom-fields','comments','revisions','page-attributes')
    		'supports' => array('title','editor','comments','thumbnail','custom-fields',),
    	);
    	register_post_type( 'product', $args );
    }
    add_action( 'init', 'my_custom_post_product' );
    function my_taxonomies_product() {
    	$labels = array(
    		'name'              => _x( 'Product Categories', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Product Category', 'taxonomy singular name' ),
    		'search_items'      => __( 'Search Product Categories' ),
    		'all_items'         => __( 'All Product Categories' ),
    		'parent_item'       => __( 'Parent Product Category' ),
    		'parent_item_colon' => __( 'Parent Product Category:' ),
    		'edit_item'         => __( 'Edit Product Category' ),
    		'update_item'       => __( 'Update Product Category' ),
    		'add_new_item'      => __( 'Add New Product Category' ),
    		'new_item_name'     => __( 'New Product Category' ),
    		'menu_name'         => __( 'Product Categories' ),
    	);
    	$args = array(
    		'hierarchical'      => true,
    		'labels'            => $labels,
    		'show_ui'           => true,
    		'show_admin_column' => true,
    		'query_var'         => true,
                    'rewrite'           => array( 'slug' => 'product_category' ),
    	);
    	register_taxonomy( 'product_category', array( 'product' ), $args );
    }
    add_action( 'init', 'create_book_tax');
    function create_book_tax() {
    	register_taxonomy(
    		'product_category',
    		'product',
    		array(
    			'label' => __( 'Category' ),
    			'rewrite' => array( 'slug' => 'product-cat' ),
    			'hierarchical' => true,
    		)
    	);
    }

    Please tell me, am i doing any mistake because after this my website unable to open.. :-(

    Thanks for any body who can solve it..

  2. Andrew
    Nuh uh moderator
    Posted 1 year ago #

    Look at your server's error log, anything useful in there?

  3. J M
    Member
    Posted 1 year ago #

    To avoid basic syntax issues when writing this type of code, you could use this great tool called Generate WP.

Topic Closed

This topic has been closed to new replies.

About this Topic