Custom post code not working in custom plugin - wordpress

I'm adding custom post code in our custom plugin but text and other not showing. Just blank.
Check my code and let me know what is the wrong.
function call_function_quiz() {
$quiz_labels = array(
'name' => __( 'Custom Quiz', 'custom-quiz' ),
'singular_name' => __( 'Quiz', 'custom-quiz' ),
'menu_name' => __( 'Quiz', 'custom-quiz' ),
'name_admin_bar' => __( 'Quiz', 'custom-quiz' ),
'add_new' => __( 'Add New', 'custom-quiz' ),
'add_new_item' => __( 'Add New Quiz', 'custom-quiz' ),
'new_item' => __( 'New Quiz', 'custom-quiz' ),
'edit_item' => __( 'Edit Quiz', 'custom-quiz' ),
'view_item' => __( 'View Quiz', 'custom-quiz' ),
'all_items' => __( 'All Quizzes', 'custom-quiz' ),
'search_items' => __( 'Search Quizzes', 'custom-quiz' ),
'parent_item_colon' => __( 'Parent Quiz:', 'custom-quiz' ),
'not_found' => __( 'No Quiz Found', 'custom-quiz' ),
'not_found_in_trash' => __( 'No Quiz Found In Trash', 'custom-quiz' ),
);
$quiz_args = array(
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => true,
'labels' => true,
'publicly_queryable' => true,
'exclude_from_search' => $exclude_search,
'label' => $plural_name,
'rewrite' => array( 'slug' => $cpt_slug ),
'has_archive' => $has_archive,
'supports' => array( 'title', 'author', 'comments' )
);
register_post_type( 'quiz', $quiz_args );
}
add_action( 'init', 'call_function_quiz' );
add_action( 'admin_menu', 'setup_admin_menu');
function setup_admin_menu() {
add_menu_page('Custom Quiz', 'Custom Quiz', 'manage_options', 'custom-quiz', 'call_function_quiz' );
}

Related

CPT and Custom Taxonomies - not showing up even when set to true

I am using WP, coded CPTs and coded custom taxonomies.
I have included the code used for one of the CPTs and one of the Custom Taxonomies below. (CPT code is in functions.php, taxonomy code is in it's own .php file)
Problem is - these aren't appearing in the Dashboard menu (as in, when you hover over Private Tours, there is no 'Destinations' submenu), and they aren't appearing on the All Private Tours page as a column option.
I have tried searching for solutions but I believe the code is correct! I just cannot work out why it isn't showing?
function create_post_type() {
register_post_type( 'Private Tours', // change the name
array(
'labels' => array(
'name' => __( 'Private Tours' ), // change the name
'singular_name' => __( 'Private Tour' ), // change the name
),
'public' => true,
'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => true,
'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
'has_archive' => true,
'rewrite' => array ( 'slug' => __( 'private_tours' ) ) // change the name
)
);
add_action( 'init', 'create_post_type' );
endif;
function averetourism_register_destination_taxonomy() {
register_taxonomy( 'destination', array( 'product' ), array(
'labels' => array(
'name' => _x( 'Destinations', 'Taxonomy General Name', 'averetourism' ),
'singular_name' => _x( 'Destination', 'Taxonomy Singular Name', 'averetourism' ),
'menu_name' => __( 'Destinations', 'averetourism' ),
'all_items' => __( 'All Destinations', 'averetourism' ),
'parent_item' => __( 'Parent Destination', 'averetourism' ),
'parent_item_colon' => __( 'Parent Destination:', 'averetourism' ),
'new_item_name' => __( 'New Destination Name', 'averetourism' ),
'add_new_item' => __( 'Add New Destination', 'averetourism' ),
'edit_item' => __( 'Edit Destination', 'averetourism' ),
'update_item' => __( 'Update Destination', 'averetourism' ),
'view_item' => __( 'View Destination', 'averetourism' ),
'separate_items_with_commas' => __( 'Separate destinations with commas', 'averetourism' ),
'add_or_remove_items' => __( 'Add or remove destinations', 'averetourism' ),
'choose_from_most_used' => __( 'Choose from the most used', 'averetourism' ),
'popular_items' => __( 'Popular Destinations', 'averetourism' ),
'search_items' => __( 'Search Destinations', 'averetourism' ),
'not_found' => __( 'Not Found', 'averetourism' ),
'no_terms' => __( 'No destinations', 'averetourism' ),
'items_list' => __( 'Destinations list', 'averetourism' ),
'items_list_navigation' => __( 'Destinations list navigation', 'averetourism' ),
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_menu' => true,
'menu_position' => 5,
) );
}
add_action( 'init', 'averetourism_register_destination_taxonomy', 0 ); ```
Please replace and try with the below code -
function create_post_type() {
register_post_type( 'Private Tours', // change the name
array(
'labels' => array(
'name' => __( 'Private Tours' ), // change the name
'singular_name' => __( 'Private Tour' ), // change the name
),
'public' => true,
'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => true,
'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
'has_archive' => true,
'rewrite' => array ( 'slug' => __( 'private_tours' ) ) // change the name
)
);
}
add_action( 'init', 'create_post_type' );
function averetourism_register_destination_taxonomy() {
register_taxonomy( 'destination', array( 'product' ), array(
'labels' => array(
'name' => _x( 'Destinations', 'Taxonomy General Name', 'averetourism' ),
'singular_name' => _x( 'Destination', 'Taxonomy Singular Name', 'averetourism' ),
'menu_name' => __( 'Destinations', 'averetourism' ),
'all_items' => __( 'All Destinations', 'averetourism' ),
'parent_item' => __( 'Parent Destination', 'averetourism' ),
'parent_item_colon' => __( 'Parent Destination:', 'averetourism' ),
'new_item_name' => __( 'New Destination Name', 'averetourism' ),
'add_new_item' => __( 'Add New Destination', 'averetourism' ),
'edit_item' => __( 'Edit Destination', 'averetourism' ),
'update_item' => __( 'Update Destination', 'averetourism' ),
'view_item' => __( 'View Destination', 'averetourism' ),
'separate_items_with_commas' => __( 'Separate destinations with commas', 'averetourism' ),
'add_or_remove_items' => __( 'Add or remove destinations', 'averetourism' ),
'choose_from_most_used' => __( 'Choose from the most used', 'averetourism' ),
'popular_items' => __( 'Popular Destinations', 'averetourism' ),
'search_items' => __( 'Search Destinations', 'averetourism' ),
'not_found' => __( 'Not Found', 'averetourism' ),
'no_terms' => __( 'No destinations', 'averetourism' ),
'items_list' => __( 'Destinations list', 'averetourism' ),
'items_list_navigation' => __( 'Destinations list navigation', 'averetourism' ),
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_menu' => true,
'menu_position' => 5,
) );
}
add_action( 'init', 'averetourism_register_destination_taxonomy', 0 );
Part solved -
I had to change some of the arrays to -
function create_post_type() {
register_post_type( 'Private Tours', // change the name
array(
'labels' => array(
'name' => __( 'Private Tours' ), // change the name
'singular_name' => __( 'Private Tour' ), // change the name
),
'public' => true,
'supports' => array ( 'title', 'editor', 'custom-fields', 'page-attributes', 'thumbnail' ), // do you need all of these options?
'taxonomies' => array( 'category', 'post_tag' , 'destination' ),
'hierarchical' => true,
'menu_icon' => get_bloginfo( 'template_directory' ) . "/images/icon.png",
'has_archive' => true,
'rewrite' => array ( 'slug' => __( 'private_tours' ) ) // change the name
)
);
}
add_action( 'init', 'create_post_type' );
function averetourism_register_destination_taxonomy() {
register_taxonomy( 'destination', array( 'product' , 'private_tours' ), array(
'labels' => array(
'name' => _x( 'Destinations', 'Taxonomy General Name', 'averetourism' ),
'singular_name' => _x( 'Destination', 'Taxonomy Singular Name', 'averetourism' ),
'menu_name' => __( 'Destinations', 'averetourism' ),
'all_items' => __( 'All Destinations', 'averetourism' ),
'parent_item' => __( 'Parent Destination', 'averetourism' ),
'parent_item_colon' => __( 'Parent Destination:', 'averetourism' ),
'new_item_name' => __( 'New Destination Name', 'averetourism' ),
'add_new_item' => __( 'Add New Destination', 'averetourism' ),
'edit_item' => __( 'Edit Destination', 'averetourism' ),
'update_item' => __( 'Update Destination', 'averetourism' ),
'view_item' => __( 'View Destination', 'averetourism' ),
'separate_items_with_commas' => __( 'Separate destinations with commas', 'averetourism' ),
'add_or_remove_items' => __( 'Add or remove destinations', 'averetourism' ),
'choose_from_most_used' => __( 'Choose from the most used', 'averetourism' ),
'popular_items' => __( 'Popular Destinations', 'averetourism' ),
'search_items' => __( 'Search Destinations', 'averetourism' ),
'not_found' => __( 'Not Found', 'averetourism' ),
'no_terms' => __( 'No destinations', 'averetourism' ),
'items_list' => __( 'Destinations list', 'averetourism' ),
'items_list_navigation' => __( 'Destinations list navigation', 'averetourism' ),
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_menu' => true,
'menu_position' => 5,
) );
}
add_action( 'init', 'averetourism_register_destination_taxonomy', 0 );
FOLLOW UP QUESTION:
I got the taxonomies to show up on the dashboard menu, but one of them is still not showing up on Appearance > Menus to allow me to auto add the links to the nav menu... they all have
'show_in_nav_menus' => true,
'show_in_menu' => true,
but this one taxonomy isn't showing.. any ideas?

Change CPT 'Add New Post' to CPT Name

How do I change the 'Add New Post' to reflect the name of my custom post type? Do I add something to the following code. I know it can be done...
function create_post_type() {
register_post_type( 'venues',
array(
'labels' => array(
'name' => __( 'Venues' ),
'singular_name' => __( 'Venue' )
),
);
Screenshot
In the labels array add:
'add_new' => _x( 'Add New', 'venue', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Venue', 'your-plugin-textdomain' ),
Also read the WordPress codex:
https://codex.wordpress.org/Function_Reference/register_post_type
You can use the code below to get the result expected.
<?php
$labels = array(
'name' => _x( 'Venues', 'Post Type General Name', 'text-domain' ),
'singular_name' => _x( 'Venue', 'Post Type Singular Name', 'text-domain' ),
'menu_name' => esc_html__( 'Venues', 'text-domain' ),
'parent_item_colon' => esc_html__( 'Parent Venues', 'text-domain' ),
'all_items' => esc_html__( 'All Venues', 'text-domain' ),
'view_item' => esc_html__( 'View Venues', 'text-domain' ),
'add_new_item' => esc_html__( 'Add New Venues', 'text-domain' ),
'add_new' => esc_html__( 'Add New', 'text-domain' ),
'edit_item' => esc_html__( 'Edit Venues', 'text-domain' ),
'update_item' => esc_html__( 'Update Venues', 'text-domain' ),
'search_items' => esc_html__( 'Search Venues', 'text-domain' ),
'not_found' => esc_html__( 'Not Found', 'text-domain' ),
'not_found_in_trash' => esc_html__( 'Not found in Trash', 'text-domain' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => esc_html__( 'venues', 'text-domain' ),
'description' => esc_html__( 'Venues news and reviews', 'text-domain' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'main_product_category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'venues', $args );
Hope this will help.

WordPress Custom Post Type with role and caps

I am developing real estate website using WordPress Custom Post Type, where user can publish, edit, delete their own post only. Here is my code but unable to achieve target...
function property_post_type() {
$labels = array(
'name' => _x( 'Properties', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Property', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Properties', 'text_domain' ),
'name_admin_bar' => __( 'Property', 'text_domain' ),
'archives' => __( 'Item Archives', 'text_domain' ),
'attributes' => __( 'Item Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Property', 'text_domain' ),
'add_new_item' => __( 'Add New Property', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Property', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'view_items' => __( 'View Items', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Property', 'text_domain' ),
'description' => __( 'Properties information page.', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'revisions'),
'taxonomies' => array( 'region', 'city' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-home',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => array("property", "properties"),
'map_meta_cap' => true,
);
register_post_type( 'property', $args );
}
add_action( 'init', 'property_post_type', 0 );
/****************************************
* Add custom taxonomy for Property *
****************************************/
add_action('init', 'property_categories_register');
function property_categories_register() {
$labels = array(
'name' => 'Category',
'singular_name' => 'Property Category',
'search_items' => 'Search Property Categories',
'popular_items' => 'Popular Property Categories',
'all_items' => 'All Properties Categories',
'parent_item' => 'Parent Property Category',
'edit_item' => 'Edit Property Category',
'update_item' => 'Update Property Category',
'add_new_item' => 'Add New Property Category',
'new_item_name' => 'New Property Category',
'separate_items_with_commas' => 'Separate properties categories with commas',
'add_or_remove_items' => 'Add or remove properties categories',
'choose_from_most_used' => 'Choose from most used properties categories'
);
$args = array(
'label' => 'Property Categories',
'labels' => $labels,
'public' => false,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'property', 'with_front' => true, 'hierarchical' => true ),
'query_var' => true
);
register_taxonomy( 'property_categories', 'property', $args );
}
add_action( 'init', 'property_post_type', 0 );
add_role('seller', 'Seller', array(
'publish_propety' => true,
'edit_property' => true,
'edit_others_property' => false,
'delete_property' => true,
'delete_others_property' => false,
'read_private_property' => true,
'edit_property' => true,
'delete_property' => true,
'read_property' => true,
// more standard capabilities here
'read' => true,
));
if ( current_user_can('seller') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_seller_uploads');
In users admin panel displaying all post...I want to user can publish, delete, edit only their own post only.
Attaching screenshot users admin panel dashboard..
Screen shot of users admin dashboard

Check is custom post type

I have a custom post type :
// Custom posttype Events
$labels = array(
'name' => _x('Events', 'Post Type General Name'),
'singular_name' => _x('Events', 'Post Type Singular Name'),
'menu_name' => __('Events'),
'parent_item_colon' => __('Events:'),
'all_items' => __('All Items'),
'view_item' => __('View Item'),
'add_new_item' => __('Add New Event'),
'add_new' => __('Add New'),
'edit_item' => __('Edit Item'),
'update_item' => __('Update Item'),
'search_items' => __('Search Item'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
'labels' => $labels,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
'taxonomies' => array('post_tag'),
'hierarchical' => false,
'rewrite' => array('slug' => __('events')),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type('events', $args);
And a taxonomy for custom post type Events:
// Add new "Type" taxonomy to Events
register_taxonomy('type-events', 'event', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Types', 'taxonomy general name', 'my_theme' ),
'singular_name' => _x( 'Types', 'taxonomy singular name', 'my_theme' ),
'search_items' => __( 'Search Type', 'my_theme' ),
'all_items' => __( 'All Types', 'my_theme' ),
'parent_item' => __( 'Parent Type', 'my_theme' ),
'parent_item_colon' => __( 'Parent Type:', 'my_theme' ),
'edit_item' => __( 'Edit Type', 'my_theme' ),
'update_item' => __( 'Update Type', 'my_theme' ),
'add_new_item' => __( 'Add New Type', 'my_theme' ),
'new_item_name' => __( 'New Type', 'my_theme' ),
'menu_name' => __( 'Types', 'my_theme' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'type-events',
'with_front' => false,
'hierarchical' => true
),
));
In Dashboard Admin, i create two taxonomy type event :
Taxonomies
Custom post type and taxonomy use the same template.
In file template, I want check if it is post type or taxonomy.
Currently, I use is_post_type_archive() to check, but the both return true. That is not what I need.
How to check if this is custom post type or taxonomy?
If you want to check if a post is the custom post type events within the loop you can use this:
<?php if ( get_post_type() === 'events' ) {
/* Do Stuff */
} ?>
If this is outside of the loop, you need to pass the post id into get_post_type():
<?php if ( get_post_type( $post_id ) === 'events' ) {
/* Do Stuff */
} ?>
Edit
You can test for multiple custom post types this way:
<?php if ( get_post_type() === 'events' || get_post_type() === 'promos' || get_post_type() === 'courses' ) {
/* Do Stuff */
} ?>
This is little complicated, maybe the best way is to check with is_post_type_archive() and is_tax(), before you load the themplate, but in your case you may try with get_queried_object().
Codex - get_queried_object
The returned object will have properties taxonomy, term_id, and term_taxonomy_id if this is taxonomy.
So maybe you can use something like get_queried_object()->taxonomy to determine if this is taxonomy and get its name and get_queried_object()->query_var for custom_post_type.
if ( isset( get_queried_object()->taxonomy ) {
//do taxonomy work ...
}
if ( property_exists( get_queried_object()->query_var ) {
//post things
}
function custom_post_type() {
$singular="Car";
$plural="cars";
$labels = array(
'name' => _x( $plural, 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( $singular, 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( $plural, 'text_domain' ),
'name_admin_bar' => __( $singular, 'text_domain' ),
'archives' => __( '$singular Archives', 'text_domain' ),
'attributes' => __( $singular.' Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent '.$singular.':', 'text_domain' ),
'all_items' => __( 'All '.$plural, 'text_domain' ),
'add_new_item' => __( 'Add New '.$singular, 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New '.$singular, 'text_domain' ),
'edit_item' => __( 'Edit '.$singular, 'text_domain' ),
'update_item' => __( 'Update '.$singular, 'text_domain' ),
'view_item' => __( 'View '.$singular, 'text_domain' ),
'view_items' => __( 'View '.$singular, 'text_domain' ),
'search_items' => __( 'Search '.$singular, 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into '.$singular, 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this '.$singular, 'text_domain' ),
'items_list' => __( $singular.' list', 'text_domain' ),
'items_list_navigation' => __( $singular.' list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter '.$singular.' list', 'text_domain' ),
);
$supports =array('title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats');
$texonomies =array('category', 'post_tag');
$args = array(
'label' => __( $singular, 'text_domain' ),
'description' => __( $singular.' Description', 'text_domain' ),
'labels' => $labels,
'supports' => $supports,
'taxonomies' => $texonomies,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 8,
'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' => 'page',
'menu_icon' => 'dashicons-video-alt'`enter code here`
);
register_post_type( 'post_type', $args );
}
add_action( 'init', 'custom_post_type');

Custom taxonomy slug before post name

I want that specific custom post url will look like:
http://www.foo.bar/{taxonomy-term-slug}/{postname}
Im using this code to create a custom post type and custom taxonomy
function create_post_types(){
$labels = array(
'name' => _x( "name_plural", 'Post Type General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Post Type Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( "name_plural - Parent:', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All name_plural", 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View name_plural', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'add_new' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit name_singular', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update name_singular', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search name_singular', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
'not_found_in_trash' => __( 'Not Found In Trash', 'WP_TEXTDOMAIN' ),
);
$args = array(
'label' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'menu_icon' => "icon",
'description' => __( 'Description', 'WP_TEXTDOMAIN' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'public' => true,
'capability_type' => 'page',
'query_var' => true,
'rewrite' => array('slug' => '%product_cat%','with_front' => false)
);
register_post_type( 'product', $args );
$labels_cat = array(
'name' => _x( "name_plural", 'Taxonomy General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Taxonomy Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( 'Categories', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All Categories', 'WP_TEXTDOMAIN' ),
'parent_item' => __( 'Category - Parent', 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( 'Category - Parent:', 'WP_TEXTDOMAIN' ),
'new_item_name' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit Category', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update Category', 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View Category', 'WP_TEXTDOMAIN' ),
'separate_items_with_commas' => __( 'Saparate Category With Commas', 'WP_TEXTDOMAIN' ),
'add_or_remove_items' => __( 'Add / Remove Category', 'WP_TEXTDOMAIN' ),
'choose_from_most_used' => __( 'Most Used', 'WP_TEXTDOMAIN' ),
'popular_items' => __( 'Populars', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search Category', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
);
$args_cat = array(
'labels' => $labels_cat,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => 'product_cat',
'rewrite' => array('slug' => 'product_cat' ),
'_builtin' => false
);
register_taxonomy( 'product_cat', 'product', $args_cat );
}
// Hook into the 'init' action
add_action( 'init', 'create_post_types', 0);
add_filter('post_link', 'product_permalink', 1, 3);
add_filter('post_type_link', 'product_permalink', 1, 3);
function product_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%product_cat%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'product_cat_cat');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'none';
return str_replace('%product_cat_cat%', $taxonomy_slug, $permalink);
}
The link structure that im getting is correct,
But once i try to click the link i get a 404 Page
Any thoughts ?
Please use this code
// post type product
function custom_post_products() {
$labels = array(
'name' => _x( 'Products', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add Product','News' ),
'add_new_item' => __( 'Add Product' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All products' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'No record found' ),
'not_found_in_trash' => __( 'No record found in the Trash' ),
'menu_name' => 'Products'
);
$args = array(
'labels' => $labels,
'description' => 'Mcarthur products',
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'menu_position' => 15
);
register_post_type( 'products', $args );
}
add_action( 'init', 'custom_post_products' );
add_action( 'init', 'build_taxonomies_products', 0 );
//custom texonomy
function build_taxonomies_products() {
register_taxonomy( 'products-cat', 'products', array( 'hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true ) );
}

Resources