WordPress 自定义文章类型
像素鱼丸
11-22
111
0

一,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()’ 获取分类信息。

收藏
打赏
WordPress 分页
上一篇
WordPress 调用自定义头像
下一篇

发表评论

注册不是必须的

像素鱼丸
150 文章
0 评论
4 喜欢
最新文章

宝塔 Nginx 拦截了 WordPress 返回 404 的状态

REST api 接口中,有段代码返回404,但是没有正常返回错误json: $wp_response = new \WP_REST_Response(array('error'=>'没有找到数据')); $wp_response->set_status(404); return $wp_response; 返回内容是: <html> <head><tit […]

Mirage 主题 v2.93.0 发布

更新内容: refactor 移除图片高宽比开关 refactor 移除全局的TOC生成开关 refactor 优化 header.php 中的seo模块和样式覆盖 feat 主题启用的时候,移除非当前主题注册的小工具 fix 修复分类小工具的bug feat 管理员打开后台,检查最当前设置首页布局的模块,如果缺少最新模块,就添加到隐藏模块列表中 fix 优化链接卡片样式 fix 优化热门文章样式 […]

如何使用 WordPress Setting API

使用 WordPress 的 Setting API 是在插件或主题中创建和管理设置页面的标准方式。它提供了一种结构化、安全的方式来保存和获取用户配置的选项。 ✅ 一、Setting API 简介 WordPress 的 Setting API 允许你: 创建设置页面(Settings Page) 注册设置字段(Settings Field) 验证和保存设置数据 使用表单提交来更新设置 ✅ 二、基 […]

详解 WordPress 的评论设置

好的,我们来详细梳理并总结 WordPress 中关于文章评论的两个核心控制层级:全局设置和单篇设置。理解这两者的关系(优先级)是管理网站评论的关键。 1. 全局设置 (Global Settings) —— 网站的“默认规则” 这是整个网站评论系统的总开关和默认行为准则。它决定了新发布的文章默认是什么样子的。 位置:WordPress 后台仪表盘 -> 设置 (Settings) -> […]
生成中...
扫描二维码
扫描二维码
用户登录