2016-07-01 41 views
0

我不熟悉PHP,因为我是一个新手。我试图使用这些代码行,但得到错误。什么是正确的方式来写这个:正确的方式来写这个PHP条件语句

<?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-->'); 

} 
?> 

感谢预期!

+1

你有什么错误? – RiggsFolly

+1

您需要点'.'来连接字符串。 – jeroen

+1

@VinodVT正如我所说的对你现在删除的答案的评论,那根本不是真的 – Steve

回答

3

您应该在echo调用中的字符串和函数调用之间添加点。

例如

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

删除分号 – Steve

+1

谢谢@Steve :) – Jamb000h

1

用这样的方式:

<?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

这里是你的代码看起来应该为了工作:

<?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-->';  
} 
?> 

这里是首选的解决方案:

<?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  } 

?> 

正如你可以看到它稍微改正了Ravi的解决方案离子。我不能说它更好,但我更喜欢它,因为它更清晰。

还有一件事。请勿使用the_author_description()功能,而应使用the_author_meta('description')

0

使用逗号避免字符串连接。此外,get_the_author_meta()同时用作get_the_author_id()和the_author_description()已被弃用。

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-->'; 
}