Creating a Permalink Structure for my Custom Content Type

Hello!
I'm trying to register a new custom content and a new taxonomy for my production website, I want it's permalinks to work as follows:

I'll give you a real example now for more clarification:
I have a content type called "CP" to display computer programs, and I created a taxonomy called "CpCat" for it's categories:
I want to display the full archive of the content type in this page: http://example.com/cp
Also, I want to create some categories, like Downloaders, Brousers, and so on, so the links will be like this: http://example.com/cp/downloaders, http://example.com/cp/browsers
In these categories, I'll add my posts, so the final result will be: http://example.com/cp/downloaders/idm, http://example.com/cp/browsers/firefox, ETC...
I've created a plugin to do that for me, but I'm getting a blank page when trying to add any new custom post now, I'm not a PHP programmer, but I've gathered this code from some websites, and here it is:

<?php
/*Plugin Name: Computer Programs Post Type
Description: This plugin registers the 'Computer Programs' post type.
Version: 1.0
License: GPLv2
*/
function wpmudev_register_taxonomy() {
	// set up labels
	$labels = array(
		'name'              => '??????? ??????? ?????????',
		'singular_name'     => '????? ???????? ????????',
		'search_items'      => '????? ?? ??????? ??????? ?????????',
		'all_items'         => '?? ??????? ?????????',
		'edit_item'         => '????? ???????',
		'update_item'       => '????? ???????',
		'add_new_item'      => '????? ????? ????',
		'new_item_name'     => '????? ????',
		'menu_name'         => '??????? ??????? ?????????'
	);
	// register taxonomy
	register_taxonomy( 'cpcat', 'cp', array(
		'hierarchical' => true,
		'labels' => $labels,
		'rewrite' => array( 'slug' => 'cp', 'with_front' => false ),
		'query_var' => true,
'show_admin_column' => true
	) );
}
add_action( 'init', 'wpmudev_register_taxonomy' );
function wpmudev_create_post_type() {
	// set up labels
	$labels = array(
 		'name' => '?????',
    	'singular_name' => '??????',
    	'add_new' => '????? ?????? ????',
    	'add_new_item' => '????? ?????? ????',
    	'edit_item' => '????? ????????',
    	'new_item' => '?????? ????',
    	'all_items' => '?? ???????',
    	'view_item' => '??? ????????',
    	'search_items' => '????? ?? ???????',
    	'not_found' =>  '?? ??? ?????? ??? ?????',
    	'not_found_in_trash' => '?? ??? ?????? ??? ????? ?? ????????',
    	'parent_item_colon' => '',
    	'menu_name' => '????? ???????',
    );
    //register post type
	register_post_type( 'cp', array(
		'labels' => $labels,
		'has_archive' => 'cp',
 		'public' => true,
		'supports' => array( 'title', 'editor', 'auther', 'excerpt', 'custom-fields', 'thumbnail','comments' ),
		'taxonomies' => array( 'post_tag', 'cpcat' ),
		'exclude_from_search' => false,
		'capability_type' => 'post',
				'rewrite' => array( 'slug' => 'cp/%cp_category%', 'with_front' => false ),
		)
	);
}
add_action( 'init', 'wpmudev_create_post_type' );
function wpa_show_permalinks( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'cp' ){
        $terms = wp_get_object_terms( $post->ID, 'cp_category' );
        if( $terms ){
            return str_replace( '%cp_category%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
?>

So, can you correct that for me please? I've been searching now for two days with no luck.
Thank you, and I'm really sorry for writing such a long question.

  • Ash

    Hello @ABDULAZIZ ALSHMASI

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    I've created a plugin to do that for me, but I'm getting a blank page when trying to add any new custom post now

    Let's debug the blank page issue first.

    Would you please enable debug mode and check if any error is shown in that page?

    To enable debug mode please edit a line in your wp-config.php:
    Change

    define('WP_DEBUG', false);

    to

    define('WP_DEBUG', true);

    Please let us know what errors you see there.

    Cheers
    Ash

  • Vinod Dalvi

    Hi @ABDULAZIZ ALSHMASI,

    Could you please try using the following modified Computer Programs Post Type plugin code?

    After using the following code you have to to save your permalink settings to Post name and resave it several time.

    <?php
    /*Plugin Name: Computer Programs Post Type
    Description: This plugin registers the 'Computer Programs' post type.
    Version: 1.0
    License: GPLv2
    */
    
    function wpmudev_register_taxonomy() {
    	// set up labels
    	$labels = array(
    		'name'              => '??????? ??????? ?????????',
    		'singular_name'     => '????? ???????? ????????',
    		'search_items'      => '????? ?? ??????? ??????? ?????????',
    		'all_items'         => '?? ??????? ?????????',
    		'edit_item'         => '????? ???????',
    		'update_item'       => '????? ???????',
    		'add_new_item'      => '????? ????? ????',
    		'new_item_name'     => '????? ????',
    		'menu_name'         => '??????? ??????? ?????????'
    	);
    	// register taxonomy
    	register_taxonomy( 'cpcat', 'cp', array(
    		'hierarchical' => true,
    		'labels' => $labels,
    		'rewrite' => array( 'slug' => 'cp', 'with_front' => false ),
    		'query_var' => true,
    'show_admin_column' => true
    	) );
    }
    add_action( 'init', 'wpmudev_register_taxonomy' );
    function wpmudev_create_post_type() {
    	// set up labels
    	$labels = array(
     		'name' => '?????',
        	'singular_name' => '??????',
        	'add_new' => '????? ?????? ????',
        	'add_new_item' => '????? ?????? ????',
        	'edit_item' => '????? ????????',
        	'new_item' => '?????? ????',
        	'all_items' => '?? ???????',
        	'view_item' => '??? ????????',
        	'search_items' => '????? ?? ???????',
        	'not_found' =>  '?? ??? ?????? ??? ?????',
        	'not_found_in_trash' => '?? ??? ?????? ??? ????? ?? ????????',
        	'parent_item_colon' => '',
        	'menu_name' => '????? ???????',
        );
        //register post type
    	register_post_type( 'cp', array(
    		'labels' => $labels,
    		'has_archive' => 'cp',
     		'public' => true,
    		'supports' => array( 'title', 'editor', 'auther', 'excerpt', 'custom-fields', 'thumbnail','comments' ),
    		'taxonomies' => array( 'post_tag', 'cpcat' ),
    		'exclude_from_search' => false,
    		'capability_type' => 'post',
    		'rewrite' => array( 'slug' => 'cp/%cpcat%', 'with_front' => false )
    		)
    
    	);
    }
    add_action( 'init', 'wpmudev_create_post_type' );
    function wpa_show_permalinks( $post_link, $id = 0 ){
        $post = get_post($id);
        if ( is_object( $post ) && $post->post_type == 'cp' ){
            $terms = wp_get_object_terms( $post->ID, 'cpcat' );
            if( $terms ){
                return str_replace( '%cpcat%' , $terms[0]->slug , $post_link );
            }
        }
    
        return $post_link;
    }
    add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
    ?>

    Best Regards,
    Vinod Dalvi

  • ABDULAZIZ ALSHMASI

    Okay, here is another problem:
    In the theme I'm using, the user can navigate to the parent category when viewing any post.
    For example: when I view any post in the games category, I can see some links like this: home / games / the name of the post.
    When I click on any of these links, I can navigate to the page associated with it.
    Unfortunately, this does not apply on custom posts and taxonomies. The category link shows as %cpcat% rather than display the right category.
    I hope that you understand me well.
    Thanks.

  • Ash

    Hello @ABDULAZIZ ALSHMASI

    In the theme I'm using, the user can navigate to the parent category when viewing any post.
    For example: when I view any post in the games category, I can see some links like this: home / games / the name of the post.

    This is called Breadcrumb. The breadcrumb function doesn't support your custom post type and taxonomies. This is difficult to say without seeing the code.

    Would you please show us the code used for that breadcrumb? Please find the function and send us the full code of that function.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.