1

我正在寻找一个函数来从 wordpress 表(wp_terms、wp_term_relationships、wp_term_taxonomy)中获取 wordpress 类别层次结构,而不使用 wordPress 函数/模板

我基本上想获取类别(父/子)关系,然后我想将它们插入到我自己的 CMS 表中。为此,我必须知道“什么类别属于什么?(所有子子(多个)目录)

到目前为止,我做了这个:

$sql="SELECT a.term_id,a.description,a.parent,a.count,b.name,b.slug
FROM wp_term_taxonomy a INNER JOIN wp_terms b WHERE a.term_id=b.term_id
AND a.taxonomy='category';
";

$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    echo($row['name'].'<br>');
}
exit;

此功能显示所有类别,但不显示层次结构,我无法获得 Child Parent的东西..

任何人都可以帮我解决这个问题吗?

问候

4

2 回答 2

2

这并不难。首先,我会通过让它表现得像 a 来包装递归的东西RecursiveIterator

class RecursiveCategoryIterator implements RecursiveIterator {
    const ID_FIELD = 'term_id';
    const PARENT_FIELD = 'parent';

    private $_data;
    private $_root;
    private $_position = 0;

    public function __construct(array $data, $root_id = 0) {
        $this->_data = $data;
        $this->_root = $root_id;
    }

    public function valid() {
        return isset($this->_data[$this->_root][$this->_position]);
    }

    public function hasChildren() {
        $subid = $this->_data[$this->_root][$this->_position][self::ID_FIELD];
        return isset($this->_data[$subid])
            && is_array($this->_data[$subid]);
    }

    public function next() {
        $this->_position++;
    }

    public function current() {
        return $this->_data[$this->_root][$this->_position];
    }

    public function getChildren() {
        return new self($this->_data,
            $this->_data[$this->_root][$this->_position][self::ID_FIELD]);
    }

    public function rewind() {
        $this->_position = 0;
    }

    public function key() {
        return $this->_position;
    }

    public static function createFromResult($result) {
        $menu_array = array();
        while($row = mysql_fetch_assoc($result)) {
            $menu_array[$row[self::PARENT_FIELD]][] = $row;
        }

        return new self($menu_array);
    }
}

现在我为什么要这样做?首先,因为您可以重新使用 id 来显示树,或者用它做其他事情,比如将它导入到您自己的表中。其次,如果你必须测试你的代码,你可以放一些其他RecursiveIterator的作为模拟(例如 a RecursiveArrayIterator)。

现在是第二部分,word-press 数据的实际导入:

// your original query
$sql="SELECT a.term_id,a.description,a.parent,a.count,b.name,b.slug
FROM wp_term_taxonomy a INNER JOIN wp_terms b WHERE a.term_id=b.term_id
AND a.taxonomy='category';
";

$result = mysql_query($sql, $dbh);

// always test for failure
if($result === false) {
    die("query failed: ". mysql_error());
}

// create the iterator from the result set
$wpterms = RecursiveCategoryIterator::createFromResult($result);

// and import it. 
insert_it($wpterms, 0);

// the function which does all the dirty work.
function insert_it($iterator, $parent_id = 0) {
    foreach($iterator as $row) {
        // insert the row, just edit the query, and don't forget
        // to escape the values. if you have an insert function,
        // use it by all means
        $qry = 'INSERT INTO my_table (myparent, myname, ...)'
            . ' VALUES (\'' . mysql_real_escape_string($parent_id)
            . '\', \'' . mysql_real_escape_string($row['name']) . '\', ....)';

        $status = mysql_query($qry);

        if($status === false) {
            // insert failed - rollback and abort
            die("hard: " . mysql_error());
        }

        // you need to pass the id of the new row
        // so the "child rows" have their respective parent
        $cid = mysql_insert_id();

        // insert the children too
        if($iterator->hasChildren()) {
            insert_it($iterator->getChildren(), $cid);
        }
    }
}    
于 2012-10-10T07:48:30.823 回答
2

如果您想将数据迁移到不同的(甚至是定制的)CMS,您应该使用Export,它将生成一个非常易于解析和导入第三方系统的 WXR (XML) 文件。它包括所有帖子类型、分类法、元数据、附件和其他所有内容。

Working directly with the database is a pain, but if you will, it'll be easier to work on the WordPress side write the data into your other tables, rather than trying to read the data from somewhere else.

于 2012-10-10T09:52:30.950 回答