0

I have issue with making new page for this :

Category link is :

/category/category_name

But I want to be :

/custom_post_type/category_name

Custom post type code

$labels     =   array(
        'name'                  => 'Vege',
        'singular_name'         => 'Vege',
        'add_new'               => 'Add Item',
        'all_items'             => 'All Items',
        'add_new_item'          => 'Add Item',
        'edit_item'             => 'Edit Item',
        'new_item'              => 'New Item',
        'view_item'             => 'View Item',
        'search_item'           => 'Search Recepti',
        'not_found'             => 'No Items found',
        'not_found_in_trash'    => 'No items found in trash',
        'parent_item_colon'     => 'Parent Item'
    );

    $args       =   array(
        'labels'                => $labels,
        'public'                => true,
        'has_archive'           => true,
        'publicly_queryable'    => true,
        'query_var'             => true,
        'rewrite'               => true,
        'capability_type'       => 'post',
        'hierarchical'          => false,
        'supports'              => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'revisions'
        ),
        'taxonomies'            => array(
            'category',
            'post_tag'
        ),
        'menu_position'         => 100,
        'menu_icon'             => 'dashicons-wordpress',
        'exclude_from_search'   => false
    );
    register_post_type( 'vege', $args );

Permalinks

http://prntscr.com/ayfqaf

http://prntscr.com/ayfq03

Is it even possible?

  • Like this? /%postname%/%category%/ = /name+post/category+name – Victor Gomes Apr 29 '16 at 19:06
  • @VictorGomes thank you for the answer, but i dont want name of post, i want custom post type name – shapic94 Apr 29 '16 at 19:17
  • Why don't you post the code you used to create the custom post type, and also your permalink structure. – Howard E Apr 29 '16 at 19:17
  • @HowardE I post everything – shapic94 Apr 29 '16 at 19:41
  • I removed my previous answer, as that's not what you're after. It sounds like you want a loop for the category archive? You'll also need a page template to be the category-archive page. – Howard E Apr 29 '16 at 21:43
0

Hopefully this is what you were asking for?

Make sure you update your permalinks (just click update) afterwards.

This will do yourwebsite.com/vege/%vege-category%/%post%

In your example, you didn't register the taxonomy type, and additionally you need to add the function to put the category slug into the permalink.

function my_custom_post_vege() {
    $labelsvege = array(
         'name'                 => 'Vege',
        'singular_name'         => 'Vege',
        'add_new'               => 'Add Item',
        'all_items'             => 'All Items',
        'add_new_item'          => 'Add Item',
        'edit_item'             => 'Edit Item',
        'new_item'              => 'New Item',
        'view_item'             => 'View Item',
        'search_item'           => 'Search Recepti',
        'not_found'             => 'No Items found',
        'not_found_in_trash'    => 'No items found in trash',
        'parent_item_colon'     => 'Parent Item'
    );
    $argsvege = array(
        'labels'        => $labelsvege,
        'description'   => 'What type of Vege this is',
        'public'        => true,
        'menu_position' => 100,
        'menu_icon'     => 'dashicons-wordpress',
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
        'has_archive'   => true,
        'hierarchical'  => true,
        'rewrite'       => array('slug' => 'vege/%vege-category%','with_front' => false),
        'query_var'     => true,
        'show_in_nav_menus'  => TRUE,
        'show_in_menu'  => TRUE,
        'label'         => 'Veges',
        'publicly_queryable'  => TRUE
    );
    register_post_type( 'vege', $argsvege );
}

add_action( 'init', 'my_custom_post_vege' );

function my_taxonomies_vegetype() {
    $labelsTaxvegetype = array(
        'name'              => _x( 'Vege Category', 'taxonomy general name' ),
        'singular_name'     => _x( 'Vege Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search vege Category' ),
        'all_items'         => __( 'All vege Category' ),
        'parent_item'       => __( 'Parent Item' ),
        'parent_item_colon' => __( 'vege Type:' ),
        'edit_item'         => __( 'Edit vege Type' ),
        'update_item'       => __( 'Update vege Type' ),
        'add_new_item'      => __( 'Add New vege Type' ),
        'new_item_name'     => __( 'New vege Type Name' ),
        'menu_name'         => __( 'vege Type' ),
    );
    $argsTaxvegetype = array(
        'labels' => $labelsTaxvegetype,
        'hierarchical'  => true,
        'public'        => true,
        'query_var'     => 'category',
        'rewrite'       =>  array('slug' => 'vege' ),
        '_builtin'      => false,
    );
    register_taxonomy( 'vege-category', 'vege', $argsTaxvegetype );
}

add_action( 'init', 'my_taxonomies_vegetype', 0 );

add_filter('post_link', 'category_permalink', 1, 3);
add_filter('post_type_link', 'category_permalink', 1, 3);

function category_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%vege-category%') === FALSE) return $permalink;
        $post = get_post($post_id);
        if (!$post) return $permalink;
        $terms = wp_get_object_terms($post->ID, 'vege-category');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'no-category';

    return str_replace('%vege-category%', $taxonomy_slug, $permalink);
}
  • Thanks for the answer, lets see, i already have yourwebsite.com/vege/%vege-category%/%post% ... I want yourwebsite.com/vege/%vege-category%/ without post ( to show all posts in vege/vege-category , do you understand? – shapic94 Apr 29 '16 at 21:35
  • You want a category archive? I removed my previous answer, as that's not what you're after. – Howard E Apr 29 '16 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.