title: How to count a specific class using PHP and display it's count on a page
body: <p>I have a wordpress driven website and running a plugin which allows me to proof my clients with photos from their photo-shoot. I am trying to add a simple section to show the selected images count at the top without messing with the plugin. I went to them first and they said it was a huge thing and wasn't implemented yet.</p>
  
  <p>Having a php script to add into the display which would look at the document and count the number of classes labeled 'selected' and return a number.</p>
  
  <p>I have a gallery online that you can see and get a feel for what I am trying to accomplish. I currently have 3 images selected to show you what I am aiming for, the title for selected is already there, I just need the php snippet.</p>
  
  <p><a href="http://www.drewjohnstonphotography.ca/proofgallery/crowell" rel="nofollow noreferrer">http://www.drewjohnstonphotography.ca/proofgallery/crowell</a></p>
  
  <p>The code I have below did not work either got it from <a href="https://stackoverflow.com/questions/15654028/how-to-count-div-of-a-specific-class-using-php-domdocument">How to count div of a specific class using PHP DOMDocument</a> topic, just modified to show selected instead of quote, but this didn't work, unfortunately.</p>
  
  <pre><code>&lt;div class="grid__item"&gt;
      &lt;div class="entry__meta-box"&gt;
          &lt;span class="meta-box__title"&gt;&lt;h3&gt;Selected&lt;/h3&gt;&lt;/span&gt;
          &lt;span&gt;
  
  &lt;?php
  
  function getElementsByClassName($elements, $className) {
  $matches = array();
  foreach($elements as $element) {
  if (!$element-&gt;hasAttribute('class')) {
      continue;
  }
  $classes = preg_split('/\s+/', $element-&gt;getAttribute('class'));
  if ( ! in_array($className, $classes)) {
      continue;
  }
  $matches[] = $element;
  }
  return $matches;
  }
  
  $dom = new DOMDocument;
  $dom-&gt;loadHTML($html_string);
  $divs = getElementsByClassName($dom-&gt;getElementsByTagName('div'), 'selected');
  $length = $divs-&gt;length;
  
  ?&gt;
  &lt;/span&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>