php 如何在 Wordpress 中使用 If、If Else 和 else

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12130840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:53:58  来源:igfitidea点击:

How to use If, If Else and else in Wordpress

phpwordpressif-statement

提问by rahul sharma

I want to use If, If else and else on wordpress homepage, i am using following condition

我想在 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  ?>

I want to show thumbnail first on homepage, if thumbnail is not available then it must use first image from post as thumbnail and if there is no image in post then it use this image

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

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

can you please tell me where i am wrong because the above code is not working

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

回答by Oliamster

An alternative would be:

另一种选择是:

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

回答by Adam11

the syntax for php is as follows:

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

You only need the opening and closing php tags (<?php ... ?>) once; one before your php code and the other after. You also need to use "echo" or "print" statements. These tell your program to output the html code that will be read by your browser. The syntax for echo is as follows:

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

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

which will output the following html:

这将输出以下html:

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

You should get a book about php. www.php.net is also a very good resource. Here are links to the manual pages about if, echo, and print statements:
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

你应该得到一本关于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

edit: You can also use "elseif" to give a new condition that must be met for the next section of code. For example:

编辑:您还可以使用“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;

回答by Sepster

Refer ElseIf/Else If.

参考ElseIf/Else If

But looks like a) you're having trouble with mixing PHP and HTML incorrectly, and b) you're not sure of logic to test if post image exists (can't help with that, sorry). Try this:

但看起来 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(); ?>" />