简体   繁体   English

在WordPress中获取父类别的名称

[英]Getting the name of the parent category in wordpress

I have child categories (cities) which belongs to parent categories (countries). 我有属于父类别(国家)的子类别(城市)。 I get a list of categories (cities-countries) as below: 我得到以下类别(城市国家)的列表:

  $categor = $wpdb->get_results("select * from $wpdb->terms c,$wpdb->term_taxonomy tt  where tt.term_id=c.term_id and tt.taxonomy='hotelcategory' and c.name != 'Uncategorized' and c.name != 'Blog' $substr order by c.name");

for($i=0;$i<count($categor);$i++)
    {
 echo '"'.$categor[$i]->name.' - '.$categor[$i]->parent.'",';
    }

I use the retrieved data in jquery autocomplete, and i can get the parent category id but not the name. 我在jquery自动完成中使用检索到的数据,我可以获取父类别ID,但不能获取名称。

The problem is for example there are many cities named "Paris", so when i type in paris, i get like 8-9 cities with same name.(Picture1) What i'd like to do is to have their parent category names beside the category.(Picture2) http://s7.postimage.org/k85dhd4sr/catn.jpg 问题是,例如,有许多城市名为“巴黎”,因此当我输入巴黎时,我会得到8-9个同名城市。(图1)我想做的是在其父类别名称旁边类别。(图片2) http://s7.postimage.org/k85dhd4sr/catn.jpg

You just have the ID of the parent-term, so if you want the actual name of the term you have to fetch it. 您仅具有父项的ID,因此,如果您想获取该术语的实际名称,则必须获取它。 One way to do that is to simply join the term table again for the parent 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 />';
}

An alternative solution without any SQL could look like that: 没有任何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 />';
}

This solution is admittedly a bit more verbose, but you don't have to worry with SQL or the database layout. 诚然,此解决方案比较冗长,但是您不必担心SQL或数据库布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM