• Resolved cezarneaga

    (@cezarneaga)


    hi
    i have this code in the loop that reads the tags in a post and if it finds one in particular “Peace initiatives” it then changes the div class and the border goes grey.

    <?php
                   $post_class="post";
    			   $tags=get_the_tags($id);
                   foreach ($tags as $key => $post_type)
                   {
                      $tag_str=$post_type->name;
                      if (stripos($tag_str,'Peace initiatives')) {
                              $tag_str="Peace initiatives";
                      }
                      switch ($tag_str)
                      {
                           case 'Peace initiatives':
                               $post_class="post-peace";
                               break;
                      }
                   }
                 ?>

    after upgrading i get a crazy error sometimes, not everytime that says:

    Warning: Invalid argument supplied for foreach() in /…/wp-content/themes/pact-by-cezar/index.php on line 31

    the line is the foreach line i use above. have the $tags arguments changed in wp 2.5?
    can anyone help me out? the blog is here: http://www.pact.lk
    thanks

    C

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Do you have any posts that don’t have any tags?

    foreach ($tags as $key => $post_type)

    That code will be invalid if $tags is not an array, which it won’t be if there are no tags on a post.

    Thread Starter cezarneaga

    (@cezarneaga)

    hi thanks for the reply Otto.
    indeed on the pages i dont have. maybe also a few posts. so in search results if a result is from a page it brings this warning.

    any idea how i could correct this?
    thank you for the help.

    C

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Try this.

    ...
    if (!empty($tags)) foreach ($tags as $key => $post_type)
    ...
    Thread Starter cezarneaga

    (@cezarneaga)

    it so worked. 🙂

    thank you so much. if you’re ever planning to scuba dive in sri lanka give me a ping.

    Cezar

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘upgraded to wp and this code in the loop is returning errors’ is closed to new replies.