您只有父术语的 ID,因此如果您想要术语的实际名称,则必须获取它。一种方法是简单地再次为parent
ID 加入术语表:
$categor = $wpdb->get_results( "select c.*,tt.*,pc.name as parent_name
from $wpdb->terms c,$wpdb->term_taxonomy tt,$wpdb->terms pc
where
tt.term_id=c.term_id and tt.parent=pc.term_id
and tt.taxonomy='hotelcategory' and c.name != 'Uncategorized'
and c.name != 'Blog' $substr order by c.name");
// I have optimized the loop a bit. If you really need the index you can leave
// it as it is. If you don't need the index I suggest you change that to a
// foreach loop
for($i=0,$n=count($categor);$i<$n;$i++)
{
echo '"'.$categor[$i]->name.' - '.$categor[$i]->parent_name.'",<br />';
}
没有任何 SQL 的替代解决方案可能如下所示:
$excludes = array();
// build the "exclude" array. This step is necessary because
// get_terms expects the ID's of the terms to be excluded
foreach(array('Uncategorized', 'Blog') as $termName) {
$term = get_term_by('name', $termName, 'hotelcategory');
if($term) {
$excludes[] = $term->term_id;
}
}
$args = array(
'hide_empty' => false,
'exclude' => $excludes,
'name__like' => 'Paris', // replace this with your search term here
);
$categor = get_terms('hotelcategory', $args);
foreach($categor as $cat)
{
// look up the parent
$parent = get_term_by('id', $cat->parent, $cat->taxonomy);
echo '"'.$cat->name.' - '.($parent ? $parent->name : '-').'",<br />';
}
诚然,这个解决方案有点冗长,但您不必担心 SQL 或数据库布局。