admin管理员组文章数量:1415111
I had all posts under different Sub-Categories (taxonomy is 'category' / default WP) within one Category and also under Custom Taxonomy (taxonomy is 'program') something like this:
- Parent Category 1
- Parent Category 2 (*only this Category that have Custom Taxonomy)
-- Sub-Category A
--- Program v
--- Program w
-- Sub-Category B
--- Program x
--- Program y
- Parent Category 3
And I want to get the tittle of Custom Taxonomy ('Program v', 'Program w', 'Program x', etc...) for each posts and then send it via fputcsv function.
For current code as below, I can get the results as expected but it also display the title of Custom Taxonomy (in this case: 'Program v') that should have an empty object (from Sub-Category A) below new row too:
Post | Category | Sub-Category | Program
Post Title 1 | Category 2*| Sub-Category B | Program x
Post Title 2 | Category 2*| Sub-Category A | Program v
Post Title 3 | Category 3 | | Program v
Post Title 4 | Category 1 | | Program v
Desired output in csv by using fputcsv is something like this:
Post | Category | Sub-Category | Program
Post Title 1 | Category 2*| Sub-Category B | Program x
Post Title 2 | Category 2*| Sub-Category A | Program v
Post Title 3 | Category 3 | |
Post Title 4 | Category 1 | |
This is the current code:
foreach ($arr_post as $post)
{
$idpost = $post->ID;
$catList = get_the_category();
foreach($catList as $cat)
{
if ($cat->category_parent == 0)
{
$maincatList = $cat->cat_name;
}
}
$subcats = get_the_category($query->$idpost);
foreach($subcats as $subcat)
{
if (!empty($subcats))
{
$subcatList = $subcats[1]->cat_name;
}
}
$progrs = get_the_terms($query->$idpost, 'program');
foreach($progrs as $progr)
{
if (!empty($progrs))
{
$progList = $progr->name;
}
}
setup_postdata($post);
fputcsv($file, array_map("utf8_decode",
array(
get_the_title(),
$maincatList,
$subcatList,
$progList,
)
));
}
本文标签: Get Custom Taxonomy Title by Spesific Sub Category via fputcsv
版权声明:本文标题:Get Custom Taxonomy Title by Spesific Sub Category via fputcsv 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196521a2647163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论