PHP echo 简单问题;)

发布于 2024-10-18 21:49:47 字数 607 浏览 5 评论 0原文

好的,它与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

早乙女 2024-10-25 21:49:47

没有大括号 { } 的 PHP if 语句将仅计算其后的第一行。要解决此问题,

 <?php if (!empty($instance['example']))  {

echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; } ?> 

请尝试一下,看看它是否满足您的需求。我所做的只是插入大括号,以便您的 if 语句涵盖所有参数。

PHP if statements that do not have braces { } will only evaluate the first line thereafter. To resolve this,

 <?php if (!empty($instance['example']))  {

echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?><?php echo "/images/example.png /></a></li>"; } ?> 

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.

花开浅夏 2024-10-25 21:49:47

您忘记在 if 语句后面添加 : 来创建 if endif; 块。或者使用标准大括号将所有命令括在 if 语句中。

目前它仅检查 if 是否有 first echo 命令。

You forgot to add the : after the if statement to make an if 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 first echo command.

孤者何惧 2024-10-25 21:49:47

试试这个代码:

<?php if (!empty($instance['example'])) 

    echo "<li><a href=". $example ."><img src=".get_bloginfo('template_directory')."/images/example.png /></a></li>";
?>   

Try this code:

<?php if (!empty($instance['example'])) 

    echo "<li><a href=". $example ."><img src=".get_bloginfo('template_directory')."/images/example.png /></a></li>";
?>   
緦唸λ蓇 2024-10-25 21:49:47

试试这个

<?php if (!empty($instance['example'])) {

    echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
    <?php echo "/images/example.png /></a></li>"; 

   }
?>

Try this

<?php if (!empty($instance['example'])) {

    echo "<li><a href=". $example ."><img src="?><?php bloginfo('template_directory') ?>
    <?php echo "/images/example.png /></a></li>"; 

   }
?>
徒留西风 2024-10-25 21:49:47

我个人会用。

<?php 
if( !empty( $instance['example'] ) )
    echo '<li><a href="' . $example . '"><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></a></li>'; 
?>

首先我们添加那些缺失的属性引号。
其次,我们使用样式表路径来确保它指向子主题的正确位置。
第三,我们调用 get_bloginfo 来获取 echo 语句的返回值。
第四,我还在图像中添加了一个 border="0" ,链接内的图像通常不需要边框,并且还添加了一个 alt 标签,因为它至少有助于通过 HTML 验证,即使您将其留空。

I'd personally use.

<?php 
if( !empty( $instance['example'] ) )
    echo '<li><a href="' . $example . '"><img src="' . get_bloginfo('stylesheet_directory') . '/images/example.png" alt="" border="0" /></a></li>'; 
?>

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.

人心善变 2024-10-25 21:49:47

与其他人相同的答案,但具有更好格式的代码:

<?php 
if(!empty($instance['example']))
{
    echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>'; 
}
?> 

添加了括号,删除了不必要的开始/结束 php 标签,并将字符串转换为单引号,因为其中不包含需要处理的变量或特殊字符。

Same answer as others, but with better formatted code:

<?php 
if(!empty($instance['example']))
{
    echo '<li><a href=' . $example . '><img src=' . bloginfo('template_directory') . '/images/example.png /></a></li>'; 
}
?> 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文