我找到了一个代码片段来在下拉菜单中显示类别。它工作正常,但是当我打开 时define('WP_DEBUG', true);
,Wordpress 会显示上述警告。我刚开始使用 php,无法弄清楚如何解决此警告。谁能帮我解决这个问题并告诉我为什么会收到此消息?
<?php
function replace_id_for_slug($option){
$categories = get_categories("hide_empty=0");
preg_match('/value="(\d*)"/', $option[0], $matches);
$id = $matches[1]; //problem on this line!
$slug = "";
foreach($categories as $category){
if($category->cat_ID == $id){
$slug = $category->slug;
}
}
return preg_replace("/value=\"(\d*)\"/", "value=\"$slug\"", $option[0]);
}
$select = wp_dropdown_categories("show_option_none=Category&hierarchical=1&hide_empty=0&echo=0");
$select = preg_replace_callback("#<option[^>]*>[^<]*</option>#", "replace_id_for_slug", $select);
echo $select; ?>