WordPress 自定义文章类型
像素鱼丸
11-22
74
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 调用自定义头像
下一篇

发表评论

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

详解 WordPress 的评论设置

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

wp_handle_comment_submission 函数

wp_handle_comment_submission() 是 WordPress 中用于处理评论提交的核心函数之一。它通常在用户提交评论时被调用,负责验证和处理评论数据,并最终将评论插入到数据库中。 ✅ 函数作用 wp_handle_comment_submission() 的主要功能是: 验证用户提交的评论数据(如评论内容、用户名、邮箱等) 检查是否为垃圾评论(通过 Akismet 或其他过 […]

Mirage 主题 v2.89.0 发布

Mirage 主题 v2.89.0 发布 feat 懒加载图片增加灰色背景颜色 refactor 移除一些无用的js文件 fix 优化卡片列表样式 feat 如果设置里删除logo,就直接显示网站名称 fix 修复pc下拉菜单宽度的一个样式bug fix 修复未登录用户取消喜欢时产生的一个错误 fix 优化 wp_vt_star 表结构 下载地址:https://gitee.com/vthemec […]

WordPress set_transient()使用方法和实现

set_transient() 是 WordPress 中用于设置临时数据的函数,它允许你将数据存储在缓存中,这些数据会在指定的时间后自动过期。它是 WordPress 提供的 transient API 的一部分,常用于缓存数据库查询、API 响应或其他计算密集型操作的结果。 一、基本概念 1. 什么是 Transient? Transient 是 WordPress 中的一种缓存机制,类似于 […]
生成中...
扫描二维码
扫描二维码
用户登录