我将WordPress从4.2.2升级到4.5.1,它破坏了我扩展Walker类的功能。Walker扩展:
class ReturnWalker extends Walker{
public $db_fields = array( \'parent\' => \'parent\',
\'id\' => \'term_id\' );
public $show_all = FALSE;
/*
*
* @param array
* @param object
* @param int
* @param
* @return NULL
*/
public function start_el( &$output, $category, $depth = 0, $args = array(), $current_object_id = 0) {
if( !is_array($output) )
$output = array();
$id = $this->db_fields[\'id\'];
$parent = $this->db_fields[\'parent\'];
if( $depth == 0 || (!$category->$parent) ){
$output[$category->term_id] = $category;
} else if( $depth > 0){
$this->search( $output, $category->$parent, $category );
}
}
/*
*
* @param array
* @param int
* @param object
*/
private function search( &$output, $key, $category){
$id = $this->db_fields[\'id\'];
$parent = $this->db_fields[\'parent\'];
if( count($output) ){
foreach( $output as $k => &$object){
if( $k == $key ){
$object->children[$category->term_id] = $category;
return;
} else if( isset($object->children[$key]) ){
$this->search( $object->children, $category->$parent, $category );
}
}
} else if( $this->show_all){
$output[$category->$id] = $category;
}
}
/*
* sets the field names used for id and parent id
* @param array(
* \'id\' => string
* \'parent\' => string
* )
* @return NULL
*/
public function setDbFields( array $db_fields){
$this->db_fields = array_merge( $this->db_fields, $db_fields );
}
}
使用“get\\u the\\u terms()”获取检索到的类别,并将其传递给walk方法:
$breadcrumbs = new ReturnWalker;
$category = get_the_terms( $post->ID, \'category\' );
$struct = $breadcrumbs->walk( $category, 0 );
经过仔细观察,我注意到$category现在是WP\\u Term对象的数组,WP\\u Term类是final。这破坏了扩展,因为它依赖于添加属性“children”的能力,但由于类是不可变的,因此不再可能。
最终的结果是,该功能将基于帖子所使用的自定义分类法构建URL和面包屑。有没有人能很好地解决这类问题?
Update -
创建一个模仿WP\\u Terms的类,并将get\\u Terms()返回的对象复制到我的*\\u Terms类中。实例化类时传入返回的对象会命中构造函数,并生成我可以控制的属性的副本,而不用担心核心代码的更改:
$categoryTerms = get_the_terms( $post->ID, \'category\' );
foreach( $categoryTerms as $categoryTerm ){
$category[] = new MyTerm( $categoryTerm );
}
$breadcrumbs = new MyWalker;
$struct = $breadcrumbs->walk( $category, 0, array());