【问题标题】:Get all parent categories of wordpress post with PHP使用 PHP 获取 wordpress 帖子的所有父类别
【发布时间】:2013-04-22 12:59:52
【问题描述】:

给定一个类别名称,我需要获取一个 wordpress 帖子所属的所有类别的数组,包括所有父类别。

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

【问题讨论】:

    标签: php wordpress categories


    【解决方案1】:

    我想我解决了。

    $subres 失败,但循环继续。

    所以在 if 条件检查成功 $subres 之后,我补充说:

                    else
                        break;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多