• Hi,

    I just added custom post types to my wordpress install using
    register_post_type( 'mycpt', 'rewrite' => false );
    (I’ve cut out all the unnecessary code)

    Next thing I did was hacking the permalink structure to just have permalinks like http://example.com/%mycpt_id%/%post_name%/

    So now I wrote those lines
    http://pastebin.com/NauYHRhA

    but now the field $post->post_name is empty due to wordpress does not seem to create slugs. also there is no more ability to edit the slug in the wp-admin by clicking on it. nevertheless the mycpt_id gets replaced.

    thank you very much for your help.
    sorry for maybe bad English.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dannify

    (@dannify)

    When I change
    register_post_type( 'mycpt', 'rewrite' => false );
    to
    register_post_type( 'mycpt', 'rewrite' => true ); and cut out the mycpt_rewrite() and mycpt_permalink() functions, then it works. So I can edit the slug and it gets generated.

    How can I add the editing ability to my custom permalink structure?
    Any ideas anyone?

    Thread Starter dannify

    (@dannify)

    The get_sample_permalink_html() is what I need. Anyboy knows how to add it to the title bar? Like the default post type..

    When searching about custom post-type permalinks, I found this to be very helpful: http://wordpress.org/support/topic/custom-post-type-permalink-structure#post-1585606

    To have an editable slug within the permalink, just add %postname% to the custom structure, I believe any of the other built-in permalink tags will also be available.

    for example:

    function myposttype_type_rewrite() {
    
    	global $wp_rewrite;
    	$queryarg = 'post_type=myposttype&p=';
    	$wp_rewrite->add_rewrite_tag('%myposttype_id%', '([^/]+)', $queryarg);
    	$wp_rewrite->add_permastruct('myposttype', '/myposttype/%myposttype_id%/%postname%', false);
    }
    
    add_filter('post_type_link', 'myposttype_permalink', 1, 3);
    function myposttype_permalink($post_link, $id = 0, $leavename, $sample) {
    
    	global $wp_rewrite;
    	$post = &get_post($id);
    	if ( is_wp_error( $post ) )
    		return $post;
    	$newlink = $wp_rewrite->get_extra_permastruct('myposttype');
    	$newlink = str_replace("%myposttype_id%", $post->ID, $newlink);
    	$newlink = home_url(user_trailingslashit($newlink));
    	return $newlink;
    }

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘edit slug disappears when adding custom permalinks’ is closed to new replies.