1

wordpress関数/テンプレートを使用せずに、wordpressテーブル(wp_terms、wp_term_relationships、wp_term_taxonomy)から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;

この関数はすべてのカテゴリを表示しますが、階層は表示せず、子の親のものを取得できません..

誰でもこれで私を助けてくれますか?

よろしく

4

2 に答える 2

2

データを別の (さらにはカスタム ビルドの) CMS に移行する場合は、Exportを使用する必要があります。これにより、サードパーティ システムへの解析とインポートが非常に簡単な WXR (XML) ファイルが生成されます。すべての投稿タイプ、分類法、メタデータ、添付ファイル、その他すべてが含まれます。

データベースを直接操作するのは面倒ですが、WordPress 側でデータを別のテーブルに書き込む方が、別の場所からデータを読み取ろうとするよりも簡単です。

于 2012-10-10T09:52:30.950 に答える
2

それほど難しくありません。まず、次のように動作させることで、再帰的なものをラップします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)。

次に、ワードプレス データの実際のインポートです。

// 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 に答える