WordPress创建商品类型和商品分类法

要说WordPress程序对于初学者来说,个人觉得最难的就是 自定义类型 和 自定义分类法 这两个东西,基本掌握了类型和分类法,简单的WordPress 仿站建站就不成问题了。

最近小编就被WordPress这分类法给整的稀里糊涂,一般企业站那必须要 产品分类的嘛,但WordPress只有一个文章分类。文章和产品的页面肯定是不太一样的,所以要使用WordPress程序制作企业站就必须创建一个自定义文章类型和自定义分类法。

什么是 自定义文章类型 ?

WordPress给出了一个模板就是后台的文章类型,在代码中程序的形式是 post type。那么我们要创建一个区别于 post type的文章类型,就是自定义文章类型了。

创建 自定义文章类型 ?

依旧的WordPress 给出了 register_post_type 这个函数,它可以让我们自由的创建自定义文章类型。

register_post_type( $post_type, $args );

$post_type参数就是自定义文章类型的名称;$args参数用于自定义文章类型的功能,因为可以自定义的功能很多,所以通常会用下面这种格式来注册:

function my_custom_post_product() {
$labels = array(
‘name’ => _x( ‘products’, ‘post type 名称’ ),
‘singular_name’ => _x( ‘product’, ‘post type 单个 item 时的名称,因为英文有复数’ ),
‘add_new’ => _x( ‘增加产品’, ‘添加新内容的链接名称’ ),
‘add_new_item’ => __( ‘增加一个产品’ ),
‘edit_item’ => __( ‘编辑产品’ ),
‘new_item’ => __( ‘新产品’ ),
‘all_items’ => __( ‘所有产品’ ),
‘view_item’ => __( ‘查看产品’ ),
‘search_items’ => __( ‘搜索产品’ ),
‘not_found’ => __( ‘没有找到有关产品’ ),
‘not_found_in_trash’ => __( ‘回收站里面没有相关产品’ ),
‘parent_item_colon’ => ”,
‘menu_name’ => ‘Products’
);
$args = array(
‘labels’ => $labels,
‘description’ => ‘我们网站的产品信息’,
‘public’ => true,
‘menu_position’ => 5,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘excerpt’, ‘comments’ ),
‘has_archive’ => true
);
register_post_type( ‘product’, $args );
}
add_action( ‘init’, ‘my_custom_post_product’ );

创建自定义分类法

添加分类功能需要使用函数register_taxonomy(),使用方法和注册自定义文章类型函数类似,区别是多了一个参数用来关联对应的自定义文章类型。

register_taxonomy( $taxonomy, $object_type, $args );

$taxonomy 字符串型,必需,分类法的名称,用英文;

$object_type数组或字符串,必需,分类法所对应的文章类型(如前面小编创建的自定义文章类型product)

$args配置参数,可选,跟register_post_type函数的$args参数类似

function my_taxonomies_product() {
$labels = array(
‘name’ => _x( ‘产品分类’, ‘taxonomy 名称’ ),
‘singular_name’ => _x( ‘产品分类’, ‘taxonomy 单数名称’ ),
‘search_items’ => __( ‘搜索产品分类’ ),
‘all_items’ => __( ‘所有产品分类’ ),
‘parent_item’ => __( ‘该产品分类的上级分类’ ),
‘parent_item_colon’ => __( ‘该产品分类的上级分类:’ ),
‘edit_item’ => __( ‘编辑产品分类’ ),
‘update_item’ => __( ‘更新产品分类’ ),
‘add_new_item’ => __( ‘添加新的产品分类’ ),
‘new_item_name’ => __( ‘新产品分类’ ),
‘menu_name’ => __( ‘产品分类’ ),
);
$args = array(
‘labels’ => $labels,
‘public’ => true,
‘show_in_nav_menus’ => true,
‘hierarchical’ => true, //控制自定义分类法的格式,如果值是false,则将分类(category)转化成标签(tags)
‘show_ui’ => true,
‘query_var’ => true,
‘rewrite’ => true,
‘show_admin_column’ => true
);
register_taxonomy( ‘products’, ‘product’, $args );//products是该自定义分类法的名称;product是对应的自定义文章类型名称
}
add_action( ‘init’, ‘my_taxonomies_product’, 0 );

调用自定义分类法文章

自定义分类法的分类列表页面模板文件是taxonomy.php或taxonomy-{taxonomy_slug}.php,taxonomy.php是所有自定义分类法默认调用的模板文件,taxonomy-{taxonomy_slug}.php则是指定自定义分类法调用的模板文件,比如本教程中创建的自定义分类法products,使用taxonomy-products.php文件即可指定调用。

问题

在小编实际操作下发现一个问题,自定义文章类型是 product  自定义分类法是 products。

当访问产品的分类页归档页 链接显示的是: xxx.com/products;

当访问产品详情页时链接显示的却是: xxx.com/product/xx.html;

这个问题小编到目前为止依旧没有解决,欢迎大家评论帮助小编解决这个问题。

0

评论0

请先
显示验证码
没有账号?注册  忘记密码?