• Resolved capilta

    (@capilta)


    Dear,

    I create custom post type (Product) using below code. I also create two page, 1. single-product.php and archive-product.php. but when i add some post and click Url my single and archive page not point. page 404 show. I don’t know what i did wrong. My code is below

    
    function ts_post_type_product() 
    {
    
    	$labels = array(
    		'name' 					=> __('Product', 'post type general name', 'capiltathemes'),
    		'singular_name' 		=> __('Product', 'post type singular name', 'capiltathemes'),
    		'add_new' 				=> __('Add New', 'product', 'capiltathemes'),
    		'all_items' 			=> __('All Products', 'capiltathemes'),
    		'add_new_item' 			=> __('Add New Product', 'capiltathemes'),
    		'edit_item' 			=> __('Edit Product', 'capiltathemes'),
    		'new_item' 				=> __('New Product', 'capiltathemes'),
    		'view_item' 			=> __('View Product', 'capiltathemes'),
    		'search_items' 			=> __('Search Product', 'capiltathemes'),
    		'not_found' 			=> __('No Product Found', 'capiltathemes'),
    		'not_found_in_trash' 	=> __('No Product Found in Trash', 'capiltathemes'), 
    		'parent_item_colon' 	=> '',
    	);
    
    	$args = array(
    		'labels' 			=> $labels,
    		'public' 			=> true,
    		'show_ui' 			=> true,
    		'capability_type' 	=> 'post',
    		'taxonomies'        => array( 'product_cats', ),
    		'hierarchical' 		=> false,
    		'rewrite' 			=> array('slug' => 'product', 'with_front' => true),
    		'query_var' 		=> true,
    		'show_in_nav_menus' => true,
    		'menu_icon' 		=> 'dashicons-cart',
    		'supports' 			=> array('title', 'thumbnail', 'excerpt', 'editor', 'author',),
    	);
    
    	register_post_type( 'portfolio' , $args );  
    	
    	$rewrite = array(
    		'slug'                       => 'product-category',
    		'with_front'                 => true,
    		'hierarchical'               => false,
    	);
    	
    	register_taxonomy('portfolio_cats',
    		array('portfolio'), 
    		array(
    			'hierarchical' 		=> true,
    			'public'            => true,
    			'label' 			=> 'Product Categories',
    			'show_admin_column'	=> 'true',
    			'singular_label' 	=> 'Category', 
    			'query_var' 		=> true,
    			'rewrite'           =>  $rewrite,
    		)
    	);
    }
    
    add_action('init', 'ts_post_type_product');
    

    Please some one help where the problem is. Why my below URL not working

    1.https://localhost/capilta/product
    2. https://localhost/capilta/product/1

    Thanks in Advanced

Viewing 6 replies - 1 through 6 (of 6 total)
  • Did you flush your .htaccess file after creating the custom post type?

    To do so, go into settings->permalinks and just hit save.

    Lmk if that does it.

    Thread Starter capilta

    (@capilta)

    I click save button several time. But still not working.
    Any others solutions

    Try replacing your code with this:

    // Register Custom Post Type
    function register_product() {
    
    	$labels = array(
    		'name'                  => 'Products',
    		'singular_name'         => 'Product',
    		'menu_name'             => 'Products',
    		'name_admin_bar'        => 'Product',
    		'archives'              => 'Product Archives',
    		'attributes'            => 'Item Attributes',
    		'parent_item_colon'     => 'Parent Item:',
    		'all_items'             => 'All Items',
    		'add_new_item'          => 'Add New Item',
    		'add_new'               => 'Add New',
    		'new_item'              => 'New Item',
    		'edit_item'             => 'Edit Item',
    		'update_item'           => 'Update Item',
    		'view_item'             => 'View Item',
    		'view_items'            => 'View Items',
    		'search_items'          => 'Search Item',
    		'not_found'             => 'Not found',
    		'not_found_in_trash'    => 'Not found in Trash',
    		'featured_image'        => 'Featured Image',
    		'set_featured_image'    => 'Set featured image',
    		'remove_featured_image' => 'Remove featured image',
    		'use_featured_image'    => 'Use as featured image',
    		'insert_into_item'      => 'Insert into item',
    		'uploaded_to_this_item' => 'Uploaded to this item',
    		'items_list'            => 'Items list',
    		'items_list_navigation' => 'Items list navigation',
    		'filter_items_list'     => 'Filter items list',
    	);
    	$args = array(
    		'label'                 => 'Product',
    		'description'           => 'Post Type Description',
    		'labels'                => $labels,
    		'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
    		'taxonomies'            => array( 'product_category' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => true,
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'capability_type'       => 'post',
    	);
    	register_post_type( 'product', $args );
    
    }
    add_action( 'init', 'register_product', 0 );
    • This reply was modified 6 years, 12 months ago by danielfonda.
    Thread Starter capilta

    (@capilta)

    I notice,

    localhost/capilta/product page not working it not show archive-product it show 404

    but localhost/capilta/product/1 working but show on single.php but i create single-product.php on themes folder but not pointed single-product.php

    Thread Starter capilta

    (@capilta)

    Thanks @danielfonda

    It is working now

    My pleasure.

    Do note that the code doesn’t include taxonomy functionality (so product categories), but that shouldn’t be too difficult to code in (assuming you need them)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Single-{custom-post].php not working’ is closed to new replies.