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

一,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 调用自定义头像
下一篇

发表评论

注册不是必须的

像素鱼丸
160 文章
2 评论
4 喜欢
最新文章

VooShop 商城主题

主题简介 Vooshop 是一个简单精美的 WordPress 主题,支持自适应、暗黑模式、多语言等功能,可以用于企业、团体、自由职业者或更广泛的主页。 Vooshop:回归 WooCommerce 的纯粹与极速 零冗余代码,零多余功能。一个完全依赖 WooCommerce 原生能力的轻量级主题。 Vooshop 不做 WooCommerce 做不到的事,我们只负责让它跑得更快。 市面上的主题总试 […]

XZhan 主题

主题简介 XZhan 主题(中文名称:小站主题)是一款 WordPress 企业主题,支持自适应、暗黑模式等功能,可快速构建高质量的企业网站。   主题特色 支持白天与暗黑模式 自适应设计,兼容多种主流浏览器 自定义主色调 LOGO扫光动画 自定义SMTP支持 内置SEO功能 文章支持点赞、分享和海报生成 丰富的小工具 侧边栏粘性滚动 简介的主题设置面板 主题设置可导入和备份 多级子菜单 […]

StarFish 配置框架

一个轻量级的 WordPress 选项框架插件,通过配置化的方式,快速为 WordPress 主题或插件生成后台设置页面。 ✨ 特性 🎯 配置驱动 UI:只需定义数组配置,自动生成完整的表单界面 📱 多页面架构:支持多个独立的设置页面 🎨 丰富的字段类型:包含 15+ 种常用字段类型 🔗 字段依赖系统:实现字段间的联动效果 ✅ 数据验证与清理:自动进行安全清理,防止 XSS 攻击 🚀 零依赖:使用 […]

什么是幽灵按钮

“幽灵按钮”(Ghost Button)是一种常见的网页与移动应用 UI 设计模式,指背景透明(或半透明)、仅通过边框(border)和文字(text)定义的按钮,视觉上“若隐若现”,仿佛“幽灵”一般——因此得名。 核心特征: 无填充色(transparent background) 背景完全透明(或与父容器同色),不遮挡背后内容。 清晰的边框(通常 1–2px 实线) 如 border: 2px […]
生成中...
扫描二维码
扫描二维码
确认购买

您确定要购买此资源吗?

微信扫码支付

请使用微信扫描二维码完成支付

订单号:

等待支付...