0

Currently, I have this script:

<?php $test = get_categories('taxonomy=item&type=things');?>

    <?php foreach($test as $stuff) : ?>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
          var $things = <?php echo json_encode($stuff->name); ?>;
          console.log($things);
          $(".element").each(function() {
            var $state = $(this).attr("title");
            if ($things.indexOf($state) > -1) {
              $(this).addClass('current');
            }
          });
        });
        </script>
    <?php endforeach; ?>

Which is working, but I'm not sure how to get it out of the foreach loop so that it doesn't keep repeating the jQuery script over and over again.

Overall, I'm trying to get the values out from the get_categories via WordPress, but then pass the name value from the array and then check that within jQuery to add a class to specific elements within a div.

I know I'm probably going about the wrong way, so I'm completely open to suggestion if anyone knows a better, cleaner way of approaching this.

3
  • If you want it to be executed once only then use a flag ($has_executed = false) which can be toggled. Commented Oct 22, 2018 at 15:39
  • Where would that go specifically? In the foreach loop or somewhere else? Commented Oct 22, 2018 at 15:51
  • I think you're talking about not repeatedly outputting the JS code into the page, rather than not repeatedly executing it? Put the existing JS stuff into a JS function which you place elsewhere, outside the loop. Make it accept a single argument as input to the function. Then in the script block within the loop, the only JS you need is a call to this function, where you pass $things as the argument. Then you've got the function written once, but called many times. That's what functions are for, after all... Commented Oct 22, 2018 at 15:52

2 Answers 2

1

Use array_column() to get all the names. Not tested code below

<?php 
$test = get_categories('taxonomy=item&type=things');
$names = array_column($test, 'name'); ?>

<script type="text/javascript">
jQuery(document).ready(function($) {
  var $things = <?php echo json_encode($names); ?>;
  console.log($things);
  $(".element").each(function() {
    var $state = $(this).attr("title");
    if ($things.indexOf($state) > -1) {
      $(this).addClass('current');
    }
  });
});
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Actually this works out as intended, thanks! I'll keep messing with it cause I might have more data that might come back from that eventually.
0

I like to separate PHP from JS as much as possible, and prevent multiple iterations. So I handle $names in PHP, then json_encode it for the JS portion to evaluate. Code below (not tested).

<?php
  $test = get_categories('taxonomy=item&type=things');

  foreach ($test as $stuff) {
    $names[] = $stuff->name;
  }
?>
<script type="text/javascript">
  jQuery(document).ready(function($) {
    var $things = <?php echo json_encode($names); ?>;
    console.log($things);
    $(".element").each(function() {
      var $state = $(this).attr("title");
      if ($things.indexOf($state) > -1) {
        $(this).addClass('current');
      }
    });
  });
</script>

1 Comment

Ahhhh, yeah. This is also what I was wondering about. A way to loop through the array, but have it stored in a variable. This helps, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.