PHP与混合了wordpress标签的HTML混合使用输出错误

时间:2015-07-08 11:01:43

标签: php html wordpress syntax lightbox

这个问题非常具体,对于你们中的一些人来说,也许很容易回答。我已经为一个客户的网站做了2个小时的努力。 我会尽力解释它。我在Wordpress中使用了自定义字段,以便客户可以向产品添加图像。我在这样的灯箱中显示图像(带插件灯箱加):<a rel="lightbox[<?php the_field('actietitel'); ?>]" href="<?php the_field('productafbeelding2'); ?>"> </a>。但是当没有“productafbeelding2”时,HTML仍会创建该灯箱,但它的值为NULL,因此它会继续加载。我现在正尝试使用'if-else-statement'来修复它:

    <?php 
     if(the_field('actie-afbeelding2') != null) 
   "<a rel='lightbox[" . the_field('actietitel');  "]' href='" . the_field('actie-afbeelding2') .  "'> </a>"
                                                           ?> 

但由于某种原因,它不起作用(这是我的数百次尝试,如果这部分代码非常糟糕,那就很抱歉)。实际上,它可以工作,但它返回的URL而不是灯箱。但是我向你展示的第一段代码实际上是在灯箱中返回链接。

您可以自己查看:http://www.bgc-testomgeving.nl/jorn/ambachtelijk/

希望你们能帮助我!

PS:我很抱歉我的语法,我很沮丧。

4 个答案:

答案 0 :(得分:0)

也许问题是

     if(the_field('actie-afbeelding2') != null) 

可能你应该改为!=&#34;&#34;。如果条件改变顺序也是个好主意:&#34;&#34; != thefield(&#39; actie-afbeelding2&#39;)

答案 1 :(得分:0)

你应该使用。在the_field(&#39; actietitel&#39;)之后而不是;。 此外,您可能忘记呼叫回声。 试试这个

if(the_field('actie-afbeelding2') != null)  {
    echo "<a rel='lightbox[" . the_field('actietitel').  "]' href='" . the_field('actie-afbeelding2') .  "'> </a>";
}

答案 2 :(得分:0)

使用此代码

<?php 
     if(isset(get_field('actie-afbeelding2') && get_field('actie-afbeelding2')!= "") {
   echo "<a rel='lightbox[";
   the_field('actietitel');  
   echo "]' href='";
   the_field('actie-afbeelding2');
   echo "'> </a>";
}
                                                           ?>

答案 3 :(得分:0)

尝试

!empty(the_field('actie-afbeelding2'))

基本上,这将测试包含isset(the_field('actie-afbeelding2'))的{​​{1}}和!the_field('actie-afbeelding2')

this article有一个很好的表格总结。