rah*_*rma 2 php wordpress if-statement
我想在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 ?>
Run Code Online (Sandbox Code Playgroud)
我想先在首页上显示缩略图,如果缩略图不可用,则必须使用帖子中的第一张图片作为缩略图,如果帖子中没有图片,则使用该图片
http://www.technoarea.in/wp-content/themes/TA/images/TA_Logo.png
Run Code Online (Sandbox Code Playgroud)
您能告诉我我在哪里错吗,因为上面的代码不起作用
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);
}
?>
Run Code Online (Sandbox Code Playgroud)
你只需要打开和关闭 php 标签 (<?php ... ?>) 一次;一个在您的 php 代码之前,另一个在您的 php 代码之后。您还需要使用“echo”或“print”语句。这些告诉您的程序输出将由您的浏览器读取的 html 代码。echo 的语法如下:
echo "<img src='some image' alt='alt' />";
Run Code Online (Sandbox Code Playgroud)
这将输出以下html:
<img src='some image' alt='alt' />
Run Code Online (Sandbox Code Playgroud)
你应该得到一本关于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”来给出下一段代码必须满足的新条件。例如:
<?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);
}
?>
Run Code Online (Sandbox Code Playgroud)
一种替代方法是:
<?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; ?>
Run Code Online (Sandbox Code Playgroud)
请参阅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(); ?>" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36195 次 |
| 最近记录: |