您的当前位置:首页 > 知识博客 > wordpress建站

通过自定义函数快速添加 WordPress 自定义分类法

时间:2024-04-05

通过自定义函数快速添加WordPress自定义文章类型

我是一名WordPress爱好者,在这篇文章中,我想介绍一个快速添加WordPress自定义分类法的方法。相信如果你已经了解了如何自定义添加WordPress文章类型,这个方法对你来说应该非常简单。

如何通过自定义函数快速添加WordPress自定义分类法

相对于添加自定义文章类型,我们是通过官方的register_taxonomy函数来添加自定义分类法的,下面是函数的全部代码:

function create_taxs($tax_slug, $hook_type, $tax_name){
    // 自定义分类法标签
    $labels_tax = array(
        'name' => $tax_name,
        'singular_name' => $tax_name,
        'search_items' => '搜索'.$tax_name,
        'all_items' => '所有'.$tax_name,
        'parent_item' => '父级'.$tax_name,
        'parent_item_colon' => '父级'.$tax_name,
        'edit_item' => '编辑'.$tax_name,
        'update_item' => '更新'.$tax_name,
        'add_new_item' => '添加新'.$tax_name,
        'new_item_name' => '新'.$tax_name.'名称',
        'menu_name' => $tax_name,
    );

    // 自定义分类法参数
    $args_tax = array(
        'hierarchical' => true,
        'labels' => $labels_tax,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array('slug' => $tax_slug),
    );

    register_taxonomy($tax_slug, array($hook_type), $args_tax);
}

使用起来也非常简单:

create_taxs("date", 'post', "日期");

其中,date是自定义分类法的slug,post是需要关联到的文章类型(这里是默认的文章,使用其他的自定义文章类型也是ok的),日期是后台菜单里显示的自定义分类法名称。

对于普通WordPress用户来说,这可能是技巧,而对于WordPress老鸟或者PHP高手来说,这顶多算个雕虫小计吧?欢迎拍砖!


欢迎咨询/Welcome to inquire
tel/vx:18842938855
qq:1685522781
email:1685522781@qq.com

Copyright © 2019-2024 sex.cyou


Whatsapp
Powered by RRZCMS