php - 如何在 CSV 上导出 wordpress 用户 slug?
问题描述
我正在使用此代码(原始代码 [此处][1])在帖子后端创建一个按钮,以便导出和下载包含以下信息的 CSV:“帖子标题”、“URL”、“类别”、“标签”、“作者”、“作者 slug” 但我找不到导出用户 slug 的方法。
我该怎么做?我会很感激你的帮助
function admin_post_list_add_export_button( $which ) {
global $typenow;
if ( 'post' === $typenow && 'top' === $which ) {
?>
<input type="submit" name="export_all_posts" class="button button-primary" value="<?php _e('Export All Posts'); ?>" />
<?php
}
}
add_action( 'manage_posts_extra_tablenav', 'admin_post_list_add_export_button', 20, 1 );
function func_export_all_posts() {
if(isset($_GET['export_all_posts'])) {
$arg = array(
'post_type' => 'post',
'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-posts.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post Title', 'URL', 'Categories', 'Tags', 'Author', 'Author slug'));
foreach ($arr_post as $post) {
setup_postdata($post);
$categories = get_the_category();
$cats = array();
if (!empty($categories)) {
foreach ( $categories as $category ) {
$cats[] = $category->name;
}
}
$post_tags = get_the_tags();
$tags = array();
if (!empty($post_tags)) {
foreach ($post_tags as $tag) {
$tags[] = $tag->name;
}
}
fputcsv($file, array(get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags), get_the_author(), get_user_by('slug','nicename')));
}
exit();
}
}
}
add_action( 'init', 'func_export_all_posts' );
解决方案
推荐阅读
- linux - 我对 Linux 中的 DNSperf 工具中的选项有疑问
- angular - 使用 Angular 和 Jasmine/Karma 的私有方法进行测试和代码覆盖
- java - 根据java中的内部对象列表值从对象列表中删除一个元素
- c++ - 暴露最直接的基类模板名称
- flutter - 如何像这样制作自己的可滚动列?
- reactjs - 通过电容器在 Ionic 中接收发送意图
- python - 使用python生成shell脚本
- r - 用于修改列中的字符串的 R 代码在 Power BI/Query 中不起作用
- pandas - 左合并后令人困惑的熊猫索引变化
- c++ - 查找符合指定条件的文件的最快方法是什么?