0
votes

How can I maintain parent child relationship in custom post type without using any plugins in wordpress ?

For eg: when you visit this site

http://www.volunteeringnepal.com/

you can find volunteer impact menu. Under this menu I've made volunteers_impact taxonomy to categorized the volunteer impact by using Plugins called gdcpt custom post type. But I want to do this without using any plugins, and most important thing is that I want to maintain parent child relationship.

In this site, when you click

www.volunteeringnepal.com/volunteers_impact/volunteering-impact-2013

you will find volunteer impact of 2013 only. The taxonomy name is voluteers_impact, but I want to keep taxonomy name volunteer-impact which is good for SEO purposes.

While giving custom post type and taxonomy name, this plugins suggest to use only underscore to join two words.

When you click single page suppose

http://www.volunteeringnepal.com/volunteer-impact/women-empowerment-2013/

you will see the difference of post name (volunteer-impact) and taxonomy name (volunteers_impact). How can I categorized it so that url structure would be same. For eg:

volunteeringnepal.com/volunteer-impact/volunteering-impact-2013

for taxonomy and

volunteeringnepal.com/volunteer-impact/women-empowerment-2013

1

1 Answers

0
votes

Using hyphens (-) to separate words in custom taxonomies and custom post types is just bad practice. You should never use any special characters, numbers or uppercase letters in taxonomy/custom post type names as they are extemely troublesome later in naming custom templates. These breaks the template hierarchy and any custom made templates will be ignored from being used. You can go and try this out for yourself. Only underscores (_) should be used.

I always try to use single words for taxonomy and post type names as they are less prone to fail in situations like these. The are also cleaner to use.

To keep the URL structure you are after, you should create a custom rewrite rule to handle that for you

EDIT

Here is the code to add a custom taxonomy and custom post type to your theme. Just add this in your functions.php. Go and play around with the code

function event_post_example() {

register_post_type( 'event_type',
    array(
    'labels' => array(
        'name' => __('Events Posts', 'baretheme'),
        'singular_name' => __('Event Post', 'baretheme'),
        'all_items' => __('All Event Posts', 'baretheme'),
        'add_new' => __('Add New Event Post', 'baretheme'),
        'add_new_item' => __('Add New Event Type', 'baretheme'),
        'edit' => __( 'Edit', 'baretheme' ),
        'edit_item' => __('Edit Post Types', 'baretheme'),
        'new_item' => __('New Post Type', 'baretheme'),
        'view_item' => __('View Post Type', 'baretheme'),
        'search_items' => __('Search Post Type', 'baretheme'),
        'not_found' =>  __('Nothing found in the Database.', 'baretheme'),
        'not_found_in_trash' => __('Nothing found in Trash', 'baretheme'),
        'parent_item_colon' => ''
    ), /* end of arrays */
    'description' => __( 'This is the example event post type', 'baretheme' ), /* Custom Type Description */
    'public' => true,
    'publicly_queryable' => true,
    'postids_from_search' => false,
    'show_ui' => true,
    'query_var' => true,
    'menu_position' => 9,
    'menu_icon' => get_stylesheet_directory_uri() . '/library/images/custom-post-icon.png',
    'rewrite'   => array( 'slug' => 'event_type', 'with_front' => false ),
    'has_archive' => 'event_type',
    'capability_type' => 'post',
    'hierarchical' => false,

    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'sticky')
    ) /* end of options */
); /* end of register post type */

register_taxonomy( 'event_cat','event_type',
    array(
        'labels' => array(
        'name' => __( 'Event Categories', 'baretheme' ),
        'singular_name' => __( 'Event Category', 'baretheme' ),
        'search_items' =>  __( 'Search Event Categories', 'baretheme' ),
        'all_items' => __( 'All Event Categories', 'baretheme' ),
        'parent_item' => __( 'Parent Event Category', 'baretheme' ),
        'parent_item_colon' => __( 'Parent Event Category:', 'baretheme' ),
        'edit_item' => __( 'Edit Event Category', 'baretheme' ),
        'update_item' => __( 'Update Event Category', 'baretheme' ),
        'add_new_item' => __( 'Add New Event Category', 'baretheme' ),
        'new_item_name' => __( 'New Event Category Name', 'baretheme' )
    ),
    'hierarchical' => true,
    'show_admin_column' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'event-slug' ),
    )
);

register_taxonomy_for_object_type( 'event_cat', 'event_type' );

}

add_action( 'init', 'event_post_example');