Hello. There is a while that outputs news with a summary. Inside this cycle, a foreach cycle is put in, which checks to which category the news belongs and, based on this, assigns a picture to the post. Problem: if a post has 2 categories, then 2 pictures are displayed, which is not necessary. How to set the condition correctly so that the category with the required id is not taken into account? I hope clearly stated. Code:

 foreach((get_the_category()) as $category) { echo '<img src="http://сайт.ru/catimg/' . $category->cat_ID . '.png" alt="' . $category->cat_name . '" />'; } 

    1 answer 1

    Maybe so?

     foreach((get_the_category()) as $category) { if($id_hashcode!=get_the_ID()){ echo '<img src="http://сайт.ru/catimg/' . $category->cat_ID . '.png" alt="' . $category->cat_name . '" />'; $id_hashcode = get_the_ID(); } } 
    • Thank you very much, earned! :) Is it necessary to enter the id heading in get_the_ID ()? And even so and so it works. And please tell me what is id_hashcode. - ether
    • one
      Leave everything as it is) id_hashcode is a temporary variable that contains the previous id in the loop. PS Do not forget to mark the answer as correct (a checkmark is to the left of the answer) - iproger