2013-04-22 5 views
0

카테고리 이름이 주어지면 모든 상위 카테고리를 포함하여 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를 확인하는 경우 조건 후에, 나는 추가

:

   else 
        break; 
관련 문제