You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在自定义文章类型的后台edit.php页面添加内容

给自定义文章类型edit.php后台页面添加内容的方法

针对你的pdfviewer自定义文章类型,以下是几种在其后台列表页面(edit.php)添加内容的实用方法,代码可直接放到主题functions.php或自定义插件中:

1. 新增自定义列表列

如果要在文章列表里添加新的列(比如展示PDF相关信息),需要两个钩子配合:

注册自定义列

add_filter('manage_pdfviewer_posts_columns', 'add_pdfviewer_custom_columns');
function add_pdfviewer_custom_columns($columns) {
    // 在现有列末尾添加"PDF链接"列
    $columns['pdf_link'] = 'PDF链接';
    // 如需插入到指定位置(比如标题列后),可替换为以下代码:
    // $new_columns = [];
    // foreach ($columns as $key => $value) {
    //     $new_columns[$key] = $value;
    //     if ($key === 'title') {
    //         $new_columns['pdf_link'] = 'PDF链接';
    //     }
    // }
    // return $new_columns;
    return $columns;
}

填充列内容

add_action('manage_pdfviewer_posts_custom_column', 'fill_pdfviewer_custom_columns', 10, 2);
function fill_pdfviewer_custom_columns($column, $post_id) {
    if ($column === 'pdf_link') {
        // 假设用自定义字段"pdf_file_url"存储PDF地址
        $pdf_url = get_post_meta($post_id, 'pdf_file_url', true);
        echo $pdf_url ? '<a href="' . esc_url($pdf_url) . '" target="_blank">查看PDF</a>' : '未设置PDF';
    }
}

2. 在页面顶部/底部添加提示或按钮

添加顶部提示框

add_action('admin_notices', 'add_pdfviewer_page_notice');
function add_pdfviewer_page_notice() {
    global $current_screen;
    // 判断当前是否为pdfviewer的列表页面
    if ($current_screen->post_type === 'pdfviewer' && $current_screen->base === 'edit') {
        ?>
        <div class="notice notice-info is-dismissible">
            <p>这是PDF查看器专属的后台提示内容</p>
        </div>
        <?php
    }
}

在筛选栏添加按钮

如果要在分类筛选等控件旁添加自定义按钮:

add_action('restrict_manage_posts', 'add_pdfviewer_filter_button');
function add_pdfviewer_filter_button() {
    global $current_screen;
    if ($current_screen->post_type === 'pdfviewer') {
        ?>
        <button type="button" class="button">批量导出PDF链接</button>
        <?php
    }
}

3. 自定义批量操作

如果要给列表添加专属的批量操作(比如导出PDF链接):

注册批量操作选项

add_filter('bulk_actions-edit-pdfviewer', 'add_pdfviewer_bulk_actions');
function add_pdfviewer_bulk_actions($actions) {
    $actions['export_pdf_links'] = '导出PDF链接';
    return $actions;
}

处理批量操作逻辑

add_action('handle_bulk_actions-edit-pdfviewer', 'handle_pdfviewer_bulk_export', 10, 3);
function handle_pdfviewer_bulk_export($redirect_to, $action, $post_ids) {
    if ($action !== 'export_pdf_links') return $redirect_to;

    // 生成CSV文件并导出
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="pdf-links.csv"');
    $output = fopen('php://output', 'w');
    fputcsv($output, ['文章ID', 'PDF链接']);
    
    foreach ($post_ids as $post_id) {
        $pdf_url = get_post_meta($post_id, 'pdf_file_url', true);
        fputcsv($output, [$post_id, $pdf_url ?: '无']);
    }
    fclose($output);
    exit;
}

内容的提问来源于stack exchange,提问作者Md. Mehedi Hassan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:05:23