首页>>帮助中心>>香港vps服务器的WordPress创建商品类型和商品分类法

香港vps服务器的WordPress创建商品类型和商品分类法

2023/9/19 373次

香港vps服务器的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.phptaxonomy-{taxonomy_slug}.phptaxonomy.php是所有自定义分类法默认调用的模板文件,taxonomy-{taxonomy_slug}.php则是指定自定义分类法调用的模板文件,比如本教程中创建的自定义分类法products,使用taxonomy-products.php文件即可指定调用。

一诺网络香港免备案专区,提供「香港增强VPS」和「香港特惠VPS」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。香港云VPS低至29/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2