• <?php if (post_is_in_descendant_category(17)) {foreach((get_the_category()) as $childcat)
    if (cat_is_ancestor_of(17, $childcat))
    echo $childcat->cat_name;
    } else echo "-";?>

    This code works and produces no error but apparently I am wondering why there is a } but without a {. I tried to remove the } but it produces error instead. Thanks in advance.

Viewing 1 replies (of 1 total)
  • Hi,

    There is nothing wrong with your code. The only difference is that braces (parenthesis) opening are not added for foreach, inner if and the else syntax.

    PHP compiler can execute this code without any problem. But if you want the code with all the parenthesis and well formated , please see below is the formatted code.

    <?php
    	if (post_is_in_descendant_category(17))
    	 {
    	   foreach((get_the_category()) as $childcat)
    	   {
    	     if (cat_is_ancestor_of(17, $childcat)){
    	       echo $childcat->cat_name;
    	      }
    	    }
    	  } else{
    		 echo "-";
    		}
    ?>

    Thanks

Viewing 1 replies (of 1 total)
  • The topic ‘Can someone tell me what is wrong with this code?’ is closed to new replies.