post 名称前的自定义分类法 slug

Custom taxonomy slug before post name

我希望特定的自定义 post url 看起来像:

http://www.foo.bar/{taxonomy-term-slug}/{postname}

我正在使用此代码创建自定义 post 类型和自定义分类

function create_post_types(){
  $labels = array(
    'name'                => _x( "name_plural", 'Post Type General Name', 'WP_TEXTDOMAIN' ),
    'singular_name'       => _x( "name_singular", 'Post Type Singular Name', 'WP_TEXTDOMAIN' ),
    'menu_name'           => __( "name_plural", 'WP_TEXTDOMAIN' ),
    'parent_item_colon'   => __( "name_plural - Parent:', 'WP_TEXTDOMAIN' ),
                        'all_items'           => __( 'All name_plural", 'WP_TEXTDOMAIN' ),
    'view_item'           => __( 'View name_plural', 'WP_TEXTDOMAIN' ),
    'add_new_item'        => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
    'add_new'             => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
    'edit_item'           => __( 'Edit name_singular', 'WP_TEXTDOMAIN' ),
    'update_item'         => __( 'Update name_singular', 'WP_TEXTDOMAIN' ),
    'search_items'        => __( 'Search name_singular', 'WP_TEXTDOMAIN' ),
    'not_found'           => __( 'Not Found', 'WP_TEXTDOMAIN' ),
    'not_found_in_trash'  => __( 'Not Found In Trash', 'WP_TEXTDOMAIN' ),
  );
  $args = array(
    'label'               => __( "name_plural", 'WP_TEXTDOMAIN' ),
    'menu_icon'           => "icon",
    'description'         => __( 'Description', 'WP_TEXTDOMAIN' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'public'              => true,
    'capability_type'     => 'page',
    'query_var'           => true,
    'rewrite'             => array('slug' => '%product_cat%','with_front' => false)
  );

  register_post_type( 'product', $args );

  $labels_cat = array(
    'name'                       => _x( "name_plural", 'Taxonomy General Name', 'WP_TEXTDOMAIN' ),
    'singular_name'              => _x( "name_singular", 'Taxonomy Singular Name', 'WP_TEXTDOMAIN' ),
    'menu_name'                  => __( 'Categories', 'WP_TEXTDOMAIN' ),
    'all_items'                  => __( 'All Categories', 'WP_TEXTDOMAIN' ),
    'parent_item'                => __( 'Category - Parent', 'WP_TEXTDOMAIN' ),
    'parent_item_colon'          => __( 'Category - Parent:', 'WP_TEXTDOMAIN' ),
    'new_item_name'              => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
    'add_new_item'               => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
    'edit_item'                  => __( 'Edit Category', 'WP_TEXTDOMAIN' ),
    'update_item'                => __( 'Update Category', 'WP_TEXTDOMAIN' ),
    'view_item'                  => __( 'View Category', 'WP_TEXTDOMAIN' ),
    'separate_items_with_commas' => __( 'Saparate Category With Commas', 'WP_TEXTDOMAIN' ),
    'add_or_remove_items'        => __( 'Add / Remove Category', 'WP_TEXTDOMAIN' ),
    'choose_from_most_used'      => __( 'Most Used', 'WP_TEXTDOMAIN' ),
    'popular_items'              => __( 'Populars', 'WP_TEXTDOMAIN' ),
    'search_items'               => __( 'Search Category', 'WP_TEXTDOMAIN' ),
    'not_found'                  => __( 'Not Found', 'WP_TEXTDOMAIN' ),
  );
  $args_cat = array(
    'labels'                     => $labels_cat,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'product_cat',
    'rewrite'                    => array('slug' => 'product_cat' ),
    '_builtin'                   => false
  );

  register_taxonomy( 'product_cat', 'product', $args_cat );
}
// Hook into the 'init' action
add_action( 'init', 'create_post_types', 0);

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

function product_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%product_cat%') === FALSE) return $permalink;
  // Get post
  $post = get_post($post_id);
  if (!$post) return $permalink;

  // Get taxonomy terms
  $terms = wp_get_object_terms($post->ID, 'product_cat_cat');
  if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
    $taxonomy_slug = $terms[0]->slug;
  else $taxonomy_slug = 'none';

  return str_replace('%product_cat_cat%', $taxonomy_slug, $permalink);

}

我得到的link结构是正确的, 但是一旦我尝试点击 link 我得到一个 404 页面

有什么想法吗?

Please use this code
// post type product

    function custom_post_products() {
        $labels = array(
            'name'               => _x( 'Products', 'post type general name' ),
            'singular_name'      => _x( 'Product', 'post type singular name' ),
            'add_new'            => _x( 'Add Product','News' ),
            'add_new_item'       => __( 'Add Product' ),
            'edit_item'          => __( 'Edit' ),
            'new_item'           => __( 'New Product' ),
            'all_items'          => __( 'All products' ),
            'view_item'          => __( 'View' ),
            'search_items'       => __( 'Search' ),
            'not_found'          => __( 'No record found' ),
            'not_found_in_trash' => __( 'No record found in the Trash' ),
            'menu_name'          => 'Products'
        );
        $args = array(
            'labels'        => $labels,
            'description'   => 'Mcarthur products',
            'public'        => true,
            'supports'      => array( 'title', 'editor', 'thumbnail' ),
            'menu_position' => 15
        );

        register_post_type( 'products', $args );
    }
    add_action( 'init', 'custom_post_products' );


    add_action( 'init', 'build_taxonomies_products', 0 );
    //custom texonomy

    function build_taxonomies_products() {
        register_taxonomy( 'products-cat', 'products', array( 'hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true ) );
    }