Replacement for a deprecated function get_all_category_ids()
-
Please help to change this code, I tried with get_terms but I couldn’t make it work:
<label>Choose category</label> <select name="myposts" >'; $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>
-
Hello markos24ro,
I hope my fix will solve your problem or most of the problem: http://pastebin.com/H7Vxn94V
I noticed that you don’t correctly separate the html and php. Also, I don’t know if you want to echo or return the $html but added a code for that and also a comment! π
Let me know if it works. okay? π
Cheers!
CalvinThanks Calvin, that’s what I tried before, but I get this error:
Catchable fatal error: Object of class stdClass could not be converted to string in …/functions.php on line 686
Line 686 is this:
$html .= '<option value="'.$cat_id.'" '.$cat_name.'>'.$cat_name.'</option>' ;Hello markos24ro,
Add the “->term_id” to $cat_id just like this:
$cat_idto
$cat_id->term_idThe reason you are getting the error is $cat_id an object and we got confused because of its name.
Hope it helps! π
Cheers!
CalvinI’m a little bit silly, and I juts don’t get it.
Can you please update the code from http://pastebin.com/H7Vxn94V with the changes, I will give you a beer !
Thank you Calvin, now the code does not give the error, but on select instead to display all category’s names that I have on my site it is displayed only Uncategorized as many time as the number of my categories.
Hi Markos,
Same result for me.
Please check this version – https://gist.github.com/idenkov/3995f1e789c6f4bfb90fIt works fine on my local install with Twenty Twelve theme.
Let me know if this works for you. π
Thanks,
IvanThanks Ivan, now the categories are displayed right, but on click update the select form does not keep the value of the selected category.
Hello Markos,
How are you? π
You can’t see it because you are comparing the $cat_id to $category which we don’t know if initialize or set. As you can see here:
if($category == $cat_id) {Is the $category variable somewhere set in your theme?
Please advise,
CalvinYes, it is used:
$category = get_option('create-posts-from-images-category');Here it is the entire select code:
<select name="create-posts-from-images-category" >'; $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> </td> </tr> </table> <p class="submit"> <input type="hidden" name="action" value="update" /> <input type="hidden" name="page_options" value="create-posts-from-images-interval,create-posts-from-images-titlelist,create-posts-from-images-linklist,create-posts-from-images-category,create-posts-from-images-addtobody,create-posts-from-images-useimagename,create-posts-from-images-delimiter" /> <input class="button button-large button-primary" type="submit" name="Submit" value="Update" /> </p> </form>Hi markos24ro,
It seems I messed up something in the code when posting to gist, and I don’t have previous versions on this computer.
Give it a go with this code-
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Category')); ?></option> <?php $option = '<option value="' . get_option('home') . '/category/">All Categories</option>'; // change category to your custom page slug $categories = get_terms( 'category', array( 'orderby' => 'count', 'hide_empty' => 0, ) ); foreach ($categories as $category) { $option .= '<option value="'.get_option('home').'/category/'.$category->slug.'">'; $option .= $category->name; $option .= ' ('.$category->count.')'; $option .= '</option>'; } echo $option; ?> </select>This time this should work. π
Thanks,
IvanI am using this code in functions php, so it is not working
Parse error: syntax error, unexpected ‘document’ (T_STRING) in /home/wallpapersitetem/public_html/demo4/wp-content/themes/funnypics/functions.php on line 598
Line 598 it is:
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>Thanks a lot for your time, I wish you a Happy New Year !
Hi markos24ro,
Put the code in your theme files – header, footer or some other page template you will like the dropdown be showed.
Sorry should have explained this in the previous posts.
Happy new year to you too!
Ivan
The topic ‘Replacement for a deprecated function get_all_category_ids()’ is closed to new replies.