show category name in url in custom post type single page wordpress

function create_listing_post_type() {
register_post_type( 'listing',
array(
'labels' => array(
'name' => esc_html__('Listings', 'shiksha-aadhar-list'),
'singular_name' => esc_html__('Listing', 'shiksha-aadhar-list'),
'add_new' => 'Add New',
'add_new_item' => 'Add New Listing',
'edit_item' => 'Edit Listing',
'new_item' => 'New Listing',
'view_item' => 'View Listing',
'search_items' => 'Search Listings',
'not_found' =>  'Nothing Found',
'not_found_in_trash' => 'Nothing found in the Trash',
'parent_item_colon' => ''
),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => 'dashicons-building',
//'menu_icon' => get_stylesheet_directory_uri() . '/yourimage.png',
//'rewrite' => true,
//'rewrite' => array('slug' => 'listing'),
'rewrite' => array('slug' => '%education_type%', 'with_front' => false),
'capability_type' => 'post',
        'has_archive' => true,
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'revisions', 'page-attributes', 'comments'),
'taxonomies'   => array( 'listing',  'post_tag' )
)
);
}
add_action( 'init', 'create_listing_post_type' );

register_taxonomy("education_type", array("listing"), array(
"hierarchical" => true,
"label" => "Categories",
"singular_label" => "Category",
'show_ui' => true,
        'query_var' => 'education_type',
        "rewrite" => true
       // 'rewrite' => array( 'slug' => 'listings')
   
));

add_filter('post_type_link', 'education_type_permalink_structure', 10, 4);
function education_type_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%education_type%')) {
        $projectscategory_type_term = get_the_terms($post->ID, 'education_type');
        if (!empty($projectscategory_type_term))
            $post_link = str_replace('%education_type%', array_pop($projectscategory_type_term)->
            slug, $post_link);
        else
            $post_link = str_replace('%education_type%', 'uncategorized', $post_link);
    }
    return $post_link;
}

Comments

Popular posts from this blog

dynamic create multiple ckeditor onclick event using jquery

get lat and long from city name using jquery

membership registration and login system in php