I came across this nice way of showing a list of posts. It works fine as long as I use the "category" taxonomy. When that changes, it still works but an annoying message displays in the background.

It is suppossed to get the slug of the taxonomies associated with the post and add them to the class of the div

Warning: Invalid argument supplied for foreach() . On the line the foreach is located.

Thank You.

<?php $the_query = new WP_Query( 'service' ); ?>
  <?php if ( $the_query->have_posts() ) : ?>

      <div id="post-list">

          <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 

                    $termsString = ""; 
                    $termsArray = get_the_terms( $post->ID, "service" );

                      foreach ( $termsArray as $term ) {
                          $termsString .= $term->slug.' ';

                <div class="<?php echo $termsString; ?> item"></div>

                <?php endwhile;  ?>
  <?php endif; ?>

1 Answer 1


Solved., little things matter.

if ($termsArray) {

  foreach ( $termsArray as $term ) {
    $termsString .= $term->slug.' ';


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.