ワードプレスのカテゴリ (親と子) を ID と共に表示する方法を教えてください。私はそれらをこの方法で印刷したい:
Europe,United Kingdom,London (Europe は親カテゴリーで、その子は United Kingdom) 10,20,33 (これらは ID です)
ヨーロッパ、フランス、パリ 10,22,45
ヨーロッパ、フランス、カンヌ 10,22,49
このコードを試しましたが、うまくいきませんでした:
<?php
$categories = get_the_category();
$this_cat_ID = $categories[0]->cat_ID;
$this_cat_name = $categories[0]->cat_name;
$this_cat_url = get_category_link($this_cat_ID);
// get the sub category if we have them
foreach ($categories as $cat) {
$parent = $cat->category_parent;
if ($parent != 0 ){
$sub_cat_ID = $cat->cat_ID;
$sub_cat_name = $cat->cat_name;
$sub_cat_url = get_category_link($sub_cat_ID);
}
}
if (!$sub_cat_ID) {
echo $this_cat_ID;
} else {
echo $sub_cat_ID;
}
?>
あなたの助けに感謝します