我在下面有一段代码,它按链接的层次顺序列出了我的所有类别及其描述。我希望有人能更新它,以允许使用特定的自定义分类法?
function list_cats_with_desc() {
$base = wp_list_categories(\'echo=0&hide_empty=0&title_li=0&orderby=ID&order=ASC\');
// wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let\'s make use of that!
// Shouldn\'t really use regexp to parse HTML, but oh well.
// (for the curious, here\'s why: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )
$get_cat_id = \'/cat-item-[0-9]+/\';
preg_match_all($get_cat_id, $base, $cat_id);
// Let\'s prepare our category descriptions to be injected.
// Format will be <a>category-name<span>category-desc</span></a>
$inject_desc = array();
$i = 0;
foreach($cat_id[0] as $id) {
$id = trim($id,\'cat-item-\');
$id = trim($id,\'"\');
$desc = trim(strip_tags(category_description($id)),"\\n"); // For some reason, category_description returns the
// description wrapped in an unwanted paragraph tag which
// we remove with strip_tags. It also adds a newline
// which we promptly trim out.
if($desc=="") $desc = " • --- ";
$inject_desc[$i] = \'</a> <span class="cat-desc">\' . $desc . \'</span>\';
$i++;
}
// Now we inject our descriptions
$base_arr = explode("\\n", $base);
$base_i = 0;
foreach($inject_desc as $desc) {
// We check whether there\'s an occurence of "</a>"
while(strpos($base_arr[$base_i], "</a>") === false) {
$base_i++;
}
// If we find one, inject our description <span>
$base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
$base_i++;
}
$base = implode("\\n", $base_arr);
echo $base;
}
SO网友:Pete
This worked...
function list_cats_with_desc() {
$base = wp_list_categories(\'echo=0&hide_empty=0&title_li=0&orderby=ID&order=ASC&taxonomy=CUSTOM-TAXONOMY-SLUG\');
// wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let\'s make use of that!
// Shouldn\'t really use regexp to parse HTML, but oh well.
// (for the curious, here\'s why: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )
$get_cat_id = \'/cat-item-[0-9]+/\';
preg_match_all($get_cat_id, $base, $cat_id);
// Let\'s prepare our category descriptions to be injected.
// Format will be <a>category-name<span>category-desc</span></a>
$inject_desc = array();
$i = 0;
foreach($cat_id[0] as $id) {
$id = trim($id,\'cat-item-\');
$id = trim($id,\'"\');
$desc = trim(strip_tags(category_description($id)),"\\n"); // For some reason, category_description returns the
// description wrapped in an unwanted paragraph tag which
// we remove with strip_tags. It also adds a newline
// which we promptly trim out.
if($desc=="") $desc = " • --- ";
$inject_desc[$i] = \'</a> <span class="cat-desc">\' . $desc . \'</span>\';
$i++;
}
// Now we inject our descriptions
$base_arr = explode("\\n", $base);
$base_i = 0;
foreach($inject_desc as $desc) {
// We check whether there\'s an occurence of "</a>"
while(strpos($base_arr[$base_i], "</a>") === false) {
$base_i++;
}
// If we find one, inject our description <span>
$base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
$base_i++;
}
$base = implode("\\n", $base_arr);
echo $base;
}