Creating Permalink For Custom Post types |
X

Congrats, You are Subscribed to Receive Updates.

Creating Permalink For Custom Post types


In my Previous Post, I  wrote about plugins and themes to create Custom Post Type Permalinks. Here I am going to create  Custom Post type and Create Permalink for it.  Let me creating a custom post type named “recipes” and custom taxonomy “Occasions” by the following way.

custom_post_type

add_action( 'init', 'kv_register_my_types' );

function kv_register_my_types() {

	register_post_type( 'recipes',
		array(
			'labels' => array(
				'name' => __( 'Recipes' ),
				'singular_name' => __( 'Recipee' )
			),
			'public' => true,
			'has_archive' => true,
		)
	);

	register_taxonomy( 'occasion', array( 'recipes' ), array( 
			'hierarchical' => true, 
			'label' => 'Occasions'
		)
	);
}

Now we are created custom post type and taxonomy.  And than, we are going to set the permastructure for our custom post and taxonomy by using the following code.

add_action( 'wp_loaded', 'kv_add_permastructure' );

function kv_add_permastructure() {
	global $wp_rewrite;

	add_permastruct( 'occasion', 'recipes/%occasion%', false );
	add_permastruct( 'recipes', 'recipes/%occasion%/%recipes%', false );
}

After that, we have to make sure the things working properly. Because we need to make some changes in .htaccess to take effect for this custom structure. No worry, here is a simple function to linking the custom post type and taxonomy with our custom permalink.

add_filter( 'post_type_link', 'kv_recipe_permalinks', 10, 2 );

function kv_recipe_permalinks( $permalink, $post ) {
	if ( $post->post_type !== 'recipes' )
		return $permalink;

	$terms = get_the_terms( $post->ID, 'occasion' );

	if ( ! $terms )
		return str_replace( '%occasion%/', '', $permalink );

	$post_terms = array();
	foreach ( $terms as $term )
		$post_terms[] = $term->slug;

	return str_replace( '%occasion%', implode( ',', $post_terms ) , $permalink );
}

add_filter( 'term_link', 'kv_add_term_parents_to_permalinks', 10, 2 );

function kv_add_term_parents_to_permalinks( $permalink, $term ) {
	$term_parents = kv_get_term_parents( $term );

	foreach ( $term_parents as $term_parent )
		$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );

	return $permlink;
}

function kv_get_term_parents( $term, &$parents = array() ) {
	$parent = get_term( $term->parent, $term->taxonomy );

	if ( is_wp_error( $parent ) )
		return $parents;

	$parents[] = $parent;

	if ( $parent->parent )
		kv_get_term_parents( $parent, $parents );

    return $parents;
}

That’s it for custom structure  .now check whether the permalink is working for our custom post or not.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar