WordPress 自定义文章类型

一,WordPress 添加自定义文章模块

在使用 WordPress 开发时,除了使用文章类型,常常需要一个新的类型,比如,一个音频文章管理模块。这时可以使用 register_post_type 创建一个新的文章类型,这样 WordPress 后台也会产生一个音频模块,非常方便。

add_action('init', 'flame_custom_post_type');
function flame_custom_post_type()
{
    $labels = array(
        'name' => _x('音频资源', 'post type 名称'),
        'singular_name' => _x('音频', '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' => '音频'
    );
    $args   = array(
        'labels' => $labels,
        'description' => '音频信息',
        'public' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-format-audio',
        // 'menu_icon' => get_template_directory_uri() . '/assets/images/icon1.png',
        'supports' => array('title', 'author','editor', 'thumbnail', 'excerpt', 'comments'),
        'has_archive' => true,
        'show_in_rest' => true
    );
    // register_post_type('rt_audio_type', $args);
    register_post_type('audios', $args);
}

官方文档地址:https://developer.wordpress.org/reference/functions/register_post_type/

menu_icon 可以使用图片,也可以使用 WordPress 的官方图标。地址:https://developer.wordpress.org/resource/dashicons


二,添加自定义分类法

添加自定义文章以后,就可以为自定义文章添加分类了。可以为一个文章类型,添加多个分类法。代码如下:

add_action('init', 'flame_taxonomies_site', 0);
function flame_taxonomies_site()
{
    $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,
        'hierarchical' => true,
        'show_in_rest' => true
    );
    register_taxonomy('audios_category', 'audios', $args);
}

相关文档:https://developer.wordpress.org/reference/functions/register_taxonomy/


现在分类已经有了,可以在后台查看和增删改分类,如果需要在文章列表按照分类筛选文章,使用下面代码:

add_action('restrict_manage_posts', 'flame_add_admin_filters', 10, 1);
function flame_add_admin_filters($post_type)
{
    if ('audios' !== $post_type) {
        return;
    }
    $taxonomies_slugs = array( // 所有自定义分类
        'audios_category',
        // 'my_other_taxonomy'
    );
    // loop through the taxonomy filters array
    foreach ($taxonomies_slugs as $slug) {
        $taxonomy = get_taxonomy($slug);
        $selected = '';
        // if the current page is already filtered, get the selected term slug
        $selected = isset($_REQUEST[$slug]) ? $_REQUEST[$slug] : '';
        // render a dropdown for this taxonomy's terms
        wp_dropdown_categories(array(
            'show_option_all' =>  $taxonomy->labels->all_items,
            'taxonomy'        =>  $slug,
            'name'            =>  $slug,
            'orderby'         =>  'name',
            'value_field'     =>  'slug',
            'selected'        =>  $selected,
            'hierarchical'    =>  true,
        ));
    }
}

相关文档:https://developer.wordpress.org/reference/hooks/restrict_manage_posts/

三,自定义分类法模板

WordPress 自定义分类法会顺序寻找模板文件。首先会寻找taxonomy-{taxonomy}-{term}.php。如果找不到它,它将在层次结构中查找下一个文件,该文件是taxonomy-{taxonomy}.php,依此类推。自定义分类法查找模板的顺序如下:

taxonomy-{taxonomy}-{term}.php 例如,如果您的分类法称为“ audios”,而同一分类是“music”,则WordPress会查找名为taxonomy-audios-music.php的文件。

taxonomy-{taxonomy}.php 例如,当一个分类法称为“audios”时,WordPress会查找名为taxonomy-audios.php的文件。

taxonomy.php

archive.php

index.php

四,自定分类文章路径

register_post_type() 自定义分类的默认文章链接是'post-slug/postname',也就是自定义文章类型名+文章名,而我们需要的链接时 'post-slog/post_id'。

add_filter('post_type_link', 'custom_type_link', 1, 3);
function custom_type_link($link, $post = 0)
{
    if ($post->post_type == 'audios') {
        return home_url('archives/audios/' . $post->ID . '.html');
    } else {
        return $link;
    }
}

add_action('init', 'custom_type_rewrites_init');
function custom_type_rewrites_init()
{
    add_rewrite_rule('archives/audios/([0-9]+)?.html$', 'index.php?post_type=audios&p=$matches[1]', 'top');
}

保存固定链接生效以后,可以通过 “domain.com/archives/audios/935.html” 访问自定义分类法的文章。

五,获取分类信息

在默认分类列表中,使用 $cat 即可获得分类ID,使用'$category = get_term($cat)' 获取分类信息。

在自定义分类法中,$cat 是空,使用'$category = get_queried_object()' 获取分类信息。


修改时间 2023-11-25

声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
WordPress 数据库操作
WordPress 引入自定义 JavaScript 文件
get_categories() 获取所有分类
WordPress 输入安全
Node.js 18.x 开始支持内置单元测试
CSS 滚动条样式修改
WordPress 后台添加菜单
Land 主题