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

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

发表评论

注册不是必须的

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

付费资源下载时间限制有什么用?

这个功能确实挺常见的,它背后的逻辑其实不是“防君子”,而是“防小人”和“控成本”。有没有必要做,主要取决于你平台上的资源类型和你的运营阶段。 我们可以从三个角度来看看这个“10天有效期”到底有什么用: 增加倒卖和二次传播的成本(防黄牛) 这是最核心的意义。如果你的资源是虚拟商品(比如教程、源码、素材包),用户付一次钱理论上可以无限复制。 如果没有有效期: 一个人买了,转手挂到闲鱼或者别的群里卖,你 […]

退款和取消订单接口要不要合并

很多开发者在设计初期的常见思路。将“取消”和“退款”分开,从功能上看似乎很清晰,但在实际的复杂业务场景中,这种设计可能会带来一些问题。 更主流和推荐的设计是提供一个统一的“申请取消订单”接口,由后端服务根据订单的当前状态,自动路由到不同的处理逻辑。  为什么统一接口是更好的选择? 前端逻辑简化: 对于用户而言,他的诉求只有一个:“我不想要这个订单了”。无论订单是否支付,他在前端点击的都是“取消订单 […]

Mirage 主题 v3.7.0 发布

Mirage 主题 v3.7.0 发布 feat 增加拉黑用户功能 feat 移动端向下滑动时隐藏header,向上滑动时显示header 下载地址 https://gitee.com/vthemecn/mirage/releases/tag/v3.7.0 https://github.com/vthemecn/mirage/releases/tag/v3.7.0 新增功能截图
生成中...
扫描二维码
扫描二维码