php - 在 wordpress 后端为自定义帖子、分类和元创建导出到 CSV 按钮
问题描述
我正在制作一个将数据导出到 CSV 文件的按钮,目前它仅适用于自定义帖子类型和自定义元。但是当我尝试用关税出口时,它破坏了网站。
我发现的问题是当我在我的 fputcsv 数组中添加这段代码时:
get_the_terms( get_the_ID(), 'custom-tax' )
这是我的 function.php 文件:
# Adding button in backend
add_action( 'restrict_manage_posts', 'add_export_button' );
function add_export_button() {
$screen = get_current_screen();
if (isset($screen->parent_file) && ('edit.php?post_type=custom-post-type' == $screen->parent_file)) {
?>
<input type="submit" name="export_all_posts" id="export_all_posts" class="button button-primary" value="Export All Posts">
<script type="text/javascript">
jQuery(function($) {
$('#export_all_posts').insertAfter('#post-query-submit');
});
</script>
<?php
}
}
# Adding export function for custom post,
add_action( 'init', 'func_export_all_posts' );
function func_export_all_posts() {
if(isset($_GET['export_all_posts'])) {
$arg = array(
'post_type' => 'custom-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
);
global $post;
$arr_post = get_posts($arg);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('post_id', 'post_title', 'custom_meta', 'custom_tax'));
foreach ($arr_post as $post) {
setup_postdata($post);
fputcsv($file, array( get_the_id(), get_post_field( 'post_title', get_post() ), get_post_meta( get_the_ID(), 'custom_meta', true ), get_the_terms( get_the_ID(), 'custom-tax' ) ));
}
exit();
}
}
}
有人可以帮助自定义分类部分吗?谢谢你。
解决方案
推荐阅读
- rest - 如何在 Asterisk ARI 中引导发起 SIP?
- c++ - 在末尾移动一个容器的项目(双端队列)
- javascript - Mongoose deleteMany子文档和相关子文档
- ios - UITableViewCell 内的可滚动 StackView - 未更新 ScrollView ContentSize
- angular - Ionic Web 应用程序 - 后退导航使页面空白(白屏)
- mysql - 重置 mariadb 的 root 密码不起作用
- firebase - 如何在 Flutter 和 Firebase 中注册新用户?
- c# - 每次重新启动应用程序后,第一次调用 dbcontext 会冻结应用程序
- sql-server - 如何通过插入子目录处理 T-SQL 文件表中的名称配置?
- java - 如何在 GMAIL 上发送消息 JavaMail?