2013-04-22 4 views
0

Учитывая имя категории, мне нужно получить массив всех категорий, к которым принадлежит wordpress, включая все родительские категории.Получить все родительские категории сообщения Wordpress с PHP

Функции wordpress НЕ доступны.

Приведенный ниже код времени (максимальное время выполнения).

$result = mysql_query("SELECT tax.term_taxonomy_id as taxid, tax.term_id, tax.parent, 
      term.name 
      FROM wp_term_taxonomy AS tax 
      JOIN wp_terms AS term ON (term.term_id = tax.term_id) 

      WHERE term.name = '".mysql_real_escape_string($cat)."' 

      AND tax.taxonomy = 'category' 

      LIMIT 1"); 

if($result) 
{ 
     $category = mysql_fetch_assoc($result); 

     //see if there are more than one parent: 

     $categories = array(); 

     $categories[] = $category['term_id']; //lowest 

     if($category['parent'] > 0){ //there are parent categories 

      $p = $category['parent']; 

      //get all the parent categories of this category 
      while($p >= 0){ 

       $subres = mysql_query("SELECT term_id, parent 

          FROM wp_term_taxonomy 

          WHERE term_id = '".intval($p)."' 

          AND tax.taxonomy = 'category' 

          LIMIT 1"); 

       if($subres){ 

        $c = mysql_fetch_assoc($subres); 

        $categories[] = $c['term_id']; 

        if($c['parent'] >= 0) 
         $p = $c['parent']; 
        else 
         break; 
       } 

      }//while 

     } 

     var_dump($categories); 
     die(); 
} //result 

ответ

0

Я думаю, что решил.

$ subres не работает, но цикл был продолжен.

Таким образом, после, если условия для проверки наличия преуспевающим $ subres, я добавил:

   else 
        break; 
Смежные вопросы