PHP echo 简单问题;)
好的,它与 Wordpress 相关,我知道 Wordpress Stack Exchange,但我在这里问是因为这主要是一个 PHP 问题。
我希望我的代码使用 if 语句显示某些内容或不显示任何内容。
问题是我将有一个变量和 bloginfo('template_directory') 内置函数。
我这样写:
<?php if (!empty($instance['example']))
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; ?>
它工作正常,直到 $instance['example'] 不为空,当为空时 - 它仍然显示模板目录链接,包括 images/example.png。
有什么想法吗?
我尝试过“ .bloginfo('template_directory') . ”,但似乎不起作用。
Ok, it's Wordpress related and I know about Wordpress Stack Exchange, but I'm asking here because this is mostly a PHP question.
I want my code to display something or nothing using if statement.
The problem is I'm going to have a variable and bloginfo('template_directory') in-bulit function.
I wrote this:
<?php if (!empty($instance['example']))
echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; ?>
It works fine until $instance['example'] is not empty, when is - it still displays the template directory link including images/example.png.
Any ideas?
I've tried " . bloginfo('template_directory') . " but doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
没有大括号 { } 的 PHP if 语句将仅计算其后的第一行。要解决此问题,
请尝试一下,看看它是否满足您的需求。我所做的只是插入大括号,以便您的 if 语句涵盖所有参数。
PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,
Try that and see if it works for your needs. All I did was insert the braces so that your if statement spans all of your arguments.
您忘记在 if 语句后面添加
:
来创建if endif;
块。或者使用标准大括号将所有命令括在 if 语句中。目前它仅检查
if
是否有 firstecho
命令。You forgot to add the
:
after the if statement to make anif endif;
block. Alternatively use the standard curly brackets to enclose all your commands in the if statement.Currently it's only checking
if
for the firstecho
command.试试这个代码:
Try this code:
试试这个
Try this
我个人会用。
首先我们添加那些缺失的属性引号。
其次,我们使用样式表路径来确保它指向子主题的正确位置。
第三,我们调用
get_bloginfo
来获取 echo 语句的返回值。第四,我还在图像中添加了一个
border="0"
,链接内的图像通常不需要边框,并且还添加了一个 alt 标签,因为它至少有助于通过 HTML 验证,即使您将其留空。I'd personally use.
First we add those missing attribute quotes.
Second we use the stylesheet path to ensure it points to the correct location for a child theme.
Third we call
get_bloginfo
to get a return value for the echo statement.Fourth, i also added a
border="0"
to the image, borders aren't usually wanted for an image inside a link and also added an alt tag, because it will at least help pass HTML validation, even if you leave it empty.与其他人相同的答案,但具有更好格式的代码:
添加了括号,删除了不必要的开始/结束 php 标签,并将字符串转换为单引号,因为其中不包含需要处理的变量或特殊字符。
Same answer as others, but with better formatted code:
Added brackets, removed unnecessary opening/closing php tags, and converted strings to single quotes since there are no variables or special characters contained within them that need processing.