【问题标题】:How to use If, If Else and else in Wordpress如何在 Wordpress 中使用 If、If Else 和 else
【发布时间】:2012-08-26 14:18:10
【问题描述】:

我想在 wordpress 主页上使用 If、If else 和 else,我正在使用以下条件

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
}?>
<?php if else { 
<img src="<?php echo catch_that_image() ?>" width="64" height="64" alt="<?php the_title(); ?>" />
} else {
<img src="http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png" width="64" height="64" alt="<?php the_title(); ?>" />}
?>
<?php  ?>

我想首先在主页上显示缩略图,如果缩略图不可用,则必须使用帖子中的第一张图片作为缩略图,如果帖子中没有图片,则使用此图片

http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png

你能告诉我我错在哪里,因为上面的代码不起作用

【问题讨论】:

  • 1. }else if (CONDITION) { ... }else{ ... } 2. 缺少结束 } 括号
  • 听不懂,你能用上面的代码告诉我你想告诉我什么吗,thnkx
  • 老实说我认为你应该从教程开始:)
  • @knoell8504 为您提供了正确手册页的链接,这是一个好的开始
  • 我同意@PeterSzymkowski,你应该先学习编程和PHP基础。您的代码中缺少一些基本概念,如果没有这种理解,就无法“修复”。

标签: php wordpress if-statement


【解决方案1】:

另一种选择是:

<?php if ($condition) : ?>
   <p> Some html code </p> <!-- or -->
   <?php echo "some php code"; ?>
<?php else : ?>
    <p> Some html code </p> <!-- or -->
   <?php echo "some php code"; ?>
<?php endif;  ?>

【讨论】:

    【解决方案2】:

    php的语法如下:

    <?php
    if(statement that must be true)
    {
        (code to be executed when the "if" statement is true);
    }
    else
    {
        (code to be executed when the "if" statement is not true);
    }
    ?>
    

    您只需要打开和关闭 php 标签 () 一次;一个在您的 php 代码之前,另一个在您的 php 代码之后。您还需要使用“echo”或“print”语句。这些告诉您的程序输出将由您的浏览器读取的 html 代码。 echo 的语法如下:

    echo "<img src='some image' alt='alt' />";
    

    这将输出以下html:

    <img src='some image' alt='alt' />
    

    你应该得到一本关于 php 的书。 www.php.net 也是一个很好的资源。以下是有关 if、echo 和 print 语句的手册页的链接:
    http://us.php.net/manual/en/control-structures.if.php
    http://us.php.net/manual/en/function.echo.php
    http://us.php.net/manual/en/function.print.php

    编辑: 您还可以使用“elseif”来给出下一段代码必须满足的新条件。例如:

    &lt;?php
    if(condition 1)
    {
        (code to be executed if condition 1 is true);
    }
    elseif(condition 2)
    {
        (code to be executed if condition 1 is false and condition 2 is true);
    }
    else
    {
        (code to be executed if neither condition is true);
    }
    ?&gt;
    

    【讨论】:

    • 如果 IF 和 ELSE 都不为真怎么办?
    • ELSE 没有条件。如果其他条件不成立,则执行“ELSE”代码。换句话说,“else”表示对前面条件的“如果不是”。
    【解决方案3】:

    请参考ElseIf/Else If

    但是看起来 a) 您在错误地混合 PHP 和 HTML 时遇到了麻烦,并且 b) 您不确定测试帖子图像是否存在的逻辑(对此无能为力,抱歉)。试试这个:

    <?php 
    if ( has_post_thumbnail() ) :
        the_post_thumbnail();
    elseif ( /*Some logic here to test if your image exists*/ ): ?>
        <img src="<?php echo catch_that_image() ?>" width="64" height="64" alt="<?php the_title(); ?>" />
    <?php else: ?>
        <img src="http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png" width="64" height="64" alt="<?php the_title(); ?>" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多