How to replace deprecated functions from my wordpr

2019-08-15 09:32发布

The function get_all_category_ids() from WordPress it is deprecated so I need to replace with get_terms() but I can't make it work. Can you please help me to make this code to be valid on WordPress 4.0 ?

<label>Choose category</label>
<select name="mycategories" >';

$category_ids = get_all_category_ids();
foreach($category_ids as $cat_id)
{
    $cat_name = get_cat_name($cat_id);

    if($category == $cat_id)
    {
        $html .= '<option selected="selected" value="'.$cat_id.'" '.$cat_name.'>'.$cat_name.'</option>';
    } else {
        $html .= '<option value="'.$cat_id.'" '.$cat_name.'>'.$cat_name.'</option>';
    }
}
$html.= '</select>

1条回答
Ridiculous、
2楼-- · 2019-08-15 10:33

Get all post categories ordered by count.

String syntax:

$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );

Array syntax:

$categories = get_terms( 'category', array(
    'orderby'    => 'count',
    'hide_empty' => 0,
 ) );

Get all the links categories:

$mylinks_categories = get_terms( 'link_category', 'orderby=count&hide_empty=0' );

See the documentation.

查看更多
登录 后发表回答