I'm using WordPress to get_categories into a variable and then trying to get the category name from the get_categories array to then pass that into a jQuery variable to have it check against another conditional in jQuery.
So far, I'm able to get the data from get_categories correctly and then use json_encode to parse that array into a json format.
But what I'm getting hung up on is I just want the name that is coming from the get_categories array from WordPress and then pass that into jQuery to run the conditional.
I'm not sure of the proper way to get that formatted correctly so that jQuery can interpret that without getting all cranky about it.
Here's what I have so far:
Code:
<?php $test = get_categories('taxonomy=stuff&type=item'); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var $things = <?php echo json_encode($test); ?>
$(".this-div").each(function() {
var $item = $(this).attr("title");
if ($things.indexOf($item) > -1) {
$(this).addClass('current');
}
});
});
</script>
I've tried $test[0]->name but that's only going to return one of the values and not all of them.
So, I'm not sure if I'm going about this correctly or if there is a better way possibly.
foreachloop as well, but that was yielding some zany results. Plus all efforts was leading me down thejson_encodepath, which appears that it should work, but I feel that I'm missing something...if()conditional would change to something possibly likeif ($things.find(function(element){ return element.name === $item; }))Ref. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…foreachloop in the order of operations to store those names into a variable to be pulled out with jQuery.