get_categories() is undefined
-
Hi!
First i feel the need to explain that im all new to wordpress and php, so i might be a bit confused here when trying to explain the parts needed for locating my problem.
But i’ll give it a try.Im working in a widget-file, lets just say its called widgetfile.php and it is located inside widget-folder.
In this file i have a few input fields that i want to use jquery ui autocomplete with.
Autocomplete takes an array and i want that array to be filled with the categories of my site that has parent id 27.What im trying to do, which might be all wrong, is that im creating a new file called autocomplete.js.
I load this file in my widgetfile.php, and in autocomplete i put my ajax-request that goes to a new php file called get-categories.phpThis is a fresh php file with this piece of code:
——————————<?php $args = array( 'type' => 'post', 'child_of' => 27, 'parent' => '', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'hierarchical' => 1, 'exclude' => '', 'include' => '', 'number' => '', 'taxonomy' => 'category', 'pad_counts' => false ); $categories = get_categories( $args ); foreach ($categories as $category) { if ($category->name){ $catNames[] = $category->name; } } return $catNames;
——————————-
I get get_categories is undefined when doing this.
So the ajax-call is atleast working in the sence that its reaching my php-file.
My JS-code is looking like this::
autocomplete.js:
—-
jQuery(document).ready(function($) { var availableTags = $.ajax({ url: '/get-categories.php', type: 'GET', dataType: 'json', success: function(data) { console.log("success:", data); autocomplete(data); }, error: function(error) { console.log("error: ", error); } }); function autocomplete(availableTags) { $( ' #acf-field-ny_klubb' ).autocomplete({ source: availableTags, delay: 100, autoFocus: true }); } });
———————-
Where #acf-field-ny_klubb is the ID of my input-field i have in widgetfile.phpSome help would be appreciated.
Basically i’d first like to know if im at all on the right track here, of if this can be done in another way thats better.
And also ofcourse why get_categories is undefined…..Thanks!
- The topic ‘get_categories() is undefined’ is closed to new replies.