2016-07-01 4 views
0

Ich bin nicht gut mit PHP vertraut, da ich ein Neuling darin bin. Ich versuche, diese Codezeilen zu verwenden, bekomme aber Fehler. Was ist der richtige Weg, um dies zu schreiben:Proper Weg, um diese PHP bedingte Anweisung zu schreiben

<?php 
if (function_exists('wpsabox_author_box')) { 
    echo wpsabox_author_box(); 
} else { 
    echo (
      '<div class="postauthor"> 
       <div class="authorprofilepix">' 
        get_avatar(get_the_author_id() , 80); 
       '</div> 

       <div class="authorprofile"> 
        <h4>' the_author(); '</h4> 
        <p>' the_author_description(); '</p> 
       </div> 
       <div class="clearfix"></div> 

      </div><!--end postauthor-->'); 

} 
?> 

Vielen Dank im Voraus!

+1

WHat Fehler erhalten Sie? – RiggsFolly

+1

Sie benötigen den Punkt '.', um Strings zu verketten. – jeroen

+1

@VinodVT wie ich sagte auf den Kommentar zu Ihrer jetzt gelöschten Antwort, das ist einfach nicht wahr – Steve

Antwort

3

Zwischen den Strings und Funktionsaufrufen innerhalb des Echoaufrufs sollten Punkte eingefügt werden.

z.B.

echo ('string' . function() . ' string '); 
+2

Drop das Semikolon – Steve

+1

Danke @Steve :) – Jamb000h

1

Verwendung auf diese Weise:

<?php if (function_exists('wpsabox_author_box')) { 
      echo wpsabox_author_box(); 
     } else { ?> 
     <div class="postauthor"> 
      <div class="authorprofilepix">' 
      <?php echo get_avatar(get_the_author_id() , 80); ?> 
      </div> 
      <div class="authorprofile"> 
       <h4><?php echo the_author(); ?></h4> 
       <p><?php echo the_author_description(); ?></p> 
      </div> 
      <div class="clearfix"></div> 

     </div><!--end postauthor--> 

    <?php  } 

?> 
0

Hier ist, wie Sie den Code zu arbeiten, um aussehen sollte:

<?php 
if (function_exists('wpsabox_author_box')) { 
    echo wpsabox_author_box(); 
} else { 
    echo '<div class="postauthor"> 
      <div class="authorprofilepix">' . get_avatar(get_the_author_id(), 80); . '</div> 
       <div class="authorprofile"> 
        <h4>' . the_author() . '</h4> 
        <p>' . the_author_description() . '</p> 
       </div> 
       <div class="clearfix"></div> 

      </div><!--end postauthor-->';  
} 
?> 

Hier ist die bevorzugte Lösung:

<?php if (function_exists('wpsabox_author_box')) { 
      echo wpsabox_author_box(); 
     } else { ?> 
     <div class="postauthor"> 
      <div class="authorprofilepix"> 
      <?php echo get_avatar(get_the_author_id() , 80); ?> 
      </div> 
      <div class="authorprofile"> 
       <h4><?php the_author(); ?></h4> 
       <p><?php the_author_description(); ?></p> 
      </div> 
      <div class="clearfix"></div> 

     </div><!--end postauthor--> 

    <?php  } 

?> 

Wie Sie kann sehen, dass es Ravis Lösung leicht korrigiert hat Ion. Ich kann nicht sagen, dass es besser ist, aber ich bevorzuge es sehr, weil es viel klarer ist.

Eine weitere Sache. Verwenden Sie nicht the_author_description() Funktion, verwenden Sie stattdessen the_author_meta('description').

0

Verwenden Sie Kommas, um die Verkettung von Zeichenfolgen zu vermeiden. Verwenden Sie außerdem get_the_author_meta(), da sowohl get_the_author_id() als auch the_author_description() veraltet sind.

if (function_exists('wpsabox_author_box')) { 
    echo wpsabox_author_box(); 
} else { 
    echo '<div class="postauthor"> 
      <div class="authorprofilepix">', 
       get_avatar(get_the_author_meta('ID') , 80), 
      '</div> 

      <div class="authorprofile"> 
       <h4>', get_the_author(), '</h4> 
       <p>', get_the_author_meta('description'), '</p> 
      </div> 
      <div class="clearfix"></div> 

     </div><!--end postauthor-->'; 
} 
Verwandte Themen