【发布时间】:2012-11-13 06:51:01
【问题描述】:
我想为产品和产品类型创建规范的永久链接。我已经确定了自定义帖子类型和自定义分类法,但我不知道是否可以使用分类法定义永久链接。例如,我的工作流程是这样的......
- 我创建了一个名为 products 的自定义帖子类型。
- 然后我为产品类型创建自定义分类。
- 然后我添加一个名为 'Chairs' 的产品类型并添加一个名为 “红椅”到此类别。
创建此产品后,用于查看此产品的所需的永久链接结构将是 像这样格式化 ->
http://shop.com/products/chairs/red-chair
这在 wordpress 3.4 中可行吗?我的自定义帖子类型中的元框允许选择为我的自定义分类定义的产品类型,并且每个产品只会有一种类型。
如果可能的话,如果可能的话,我还想包括所选产品类别的任何父级(例如,如果 'chairs' 类别是 'lounge' 的子级 类别,永久链接结构如下 ->
http://shop.com/products/lounge/chairs/red-chair
这是我创建自定义帖子类型和自定义分类的方法,我只需要帮助定义重写/slug 规则以在永久链接中包含产品类型。
/* Custom Post Type - Products ------- */
function products_init() {
$args = array(
'public' => true,
'label' => 'Products'
);
register_post_type( 'products', $args );
}
add_action( 'init', 'products_init' );
/* Custom Taxonomy - Product Type ------- */
add_action( 'init', 'create_prodtype' );
function create_prodtype() {
$labels = array(
'name' => _x( 'Product Type', 'products' ),
'singular_name' => _x( 'Product Category', 'product' ),
'search_items' => __( 'Search Product Types' ),
'all_items' => __( 'All Product Types' ),
'parent_item' => __( 'Products' ),
'parent_item_colon' => __( 'Products:' ),
'edit_item' => __( 'Edit Product Type' ),
'update_item' => __( 'Update Product Type' ),
'add_new_item' => __( 'Add New Product Type' ),
'new_item_name' => __( 'New Product Type' ),
);
register_taxonomy(
'products',
array('products'),
array(
'rewrite' => array(
'slug' => 'products',
'hierarchical' => true
),
'with_front' => false,
'labels' => $labels
));
}
【问题讨论】:
-
能否请您阅读我前段时间写的this 文章。它几乎可以满足您的需求,只是它只支持两级分类固定链接。这意味着
http://shop.com/products/lounge/chairs/red/fancy-chair很可能不起作用。 -
嗯,尝试将它与我的代码集成,也可以单独集成。它根本没有选择“FAQ”网址前缀......
标签: wordpress custom-post-type permalinks