2017-10-25 12 views
0

Ich bekomme einen Syntaxfehler, wenn ich ein Array hinzufügen. Wird jemand darauf hinweisen, wo ich einen Fehler gemacht habe?Wie kann ich meine Wordpress Shortcode in ein Array ändern

function commresi() { 
       ob_start(); 
       ?>  
<?php if(has_term=array('commercial',’commercial-filtration’,'commercial-water-softeners’,’category')) { ?> 
     <p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p> 
<?php } else { ?> 
     <p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p> 
    <?php } ?> 

<?php 
       return ob_get_clean(); 
} 
add_shortcode('comres', 'commresi'); 

Antwort

1

vergessen, einen PHP-Tag vor ob_start zu öffnen, vergaß das Dollarzeichen ($), wenn die has_term Variable deklariert und vergessen, den PHP-Tag am Ende des Codes zu schließen.

function commresi() 
{ 
    <?php 
     ob_start(); 
    ?>  
    <?php 

     if ($has_term = array(
      'commercial', 
      ’commercial - filtration’, 
      'commercial-water-softeners’,’category' 
     )) 

    { ?> 
      <p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p> 
    <?php 
    } 
    else 
    { ?> 
      <p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p> 
     <?php 
    } ?> 

    <?php 
     return ob_get_clean(); 
    ?> 
} 
add_shortcode('comres', 'commresi'); 
+0

Bearbeitung zum Hinzufügen von ... –

0

vergessen die $ (Zeichen) vor has_term

function commresi() { 
        ob_start(); 
        ?>  
    <?php if($has_term=array('commercial',’commercial-filtration’,'commercial-water-softeners’,’category')) { ?> 
      <p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p> 
    <?php } else { ?> 
      <p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p> 
     <?php } ?> 

    <?php 
        return ob_get_clean(); 
    } 
    add_shortcode('comres', 'commresi' 

); 
0

Verwendung dieser Code Zugriffe auf Variablen:

function commresi() { 
$commercial_array = array('commercial','commercial-filtration','commercial-water-softeners','category'); 
$return = ' <p class="not-commercial com-res"><a href="/commercial">Visit Commercial Systems</a></p>'; 

if(in_array ($has_term, $commercial_array) ) { 
    $return = ' <p class="commercial com-res"><a href="/home">Visit Residential Systems</a></p>'; 
} 
return $return; 
} 

add_shortcode('comres', 'commresi'); 

wo $ has_term ist Begriff, den Sie von $ commercial_array übereinstimmen soll.

Verwandte Themen