0

I need to do a "if" inside my json, but it is showing in my "if"

"syntax error, unexpected 'if' (T_IF), expecting ',' or ';'"

Could you please help me?

<?php 
    $cat_video = get_the_category();
    foreach($cat_video as $c){
        if($c->slug=='videos'){
            echo '<script type="application/ld+json">{
                "@context": "https://schema.org/",
                "@type": "VideoObject",
                "name": "'.$titulo_replace.'",
                "duration": "PT1M54S","uploadDate": "'.date("Y-m-d", strtotime($date)).'",
                "thumbnailUrl": "'.$image_url.'",
                "description": "'if($excerpt){.$excerpt.}else{.$titulo_replace.}'",
                "contentUrl": "'.get_post_embed_url( get_the_ID() ).'",
                "regionsAllowed": "pt-BR"}
            </script>';
        }
    }
    ?>
brombeer
  • 8,716
  • 5
  • 21
  • 27
luisa
  • 1
  • 2
    You can not use if "inside" echo. You need to either end your echo statement, then write an if/else statement that itself also echos out the necessary value in each branch, and continue with another echo statement for the rest after; or use the ternary operator & string concatenation. – CBroe Mar 09 '22 at 15:09
  • https://stackoverflow.com/questions/3507042/if-block-inside-echo-statement – CBroe Mar 09 '22 at 15:11
  • You can use a ternary operator in place of this IF `"description": "' . $excerpt ? $excerpt : $titulo_replace . '",` – Andy Gee Mar 09 '22 at 15:28

0 Answers0