PHP与html和wordpress标签混合会得到错误的输出


PHP mixed with html mixed with wordpress tags gets wrong output

这个问题非常具体,对于你们中的一些人来说可能真的很容易回答。我正在为一个客户的网站挣扎了2个小时。我会尽量把它解释清楚。我利用Wordpress中的自定义字段,以便客户可以向产品添加图像。我在这样一个灯箱中显示图像(带有插件lightbox plus): <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,而不是在lightbox中。但是我给你看的第一段代码实际上返回了一个灯箱中的链接。

你可以在:http://www.bgc-testomgeving.nl/jorn/ambachtelijk/

希望你们能帮助我!

PS:我很抱歉我的语法,我很烂。

使用此代码

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

问题可能出在

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

也许你应该把它改成!= "。更改if条件中的顺序也是一个好主意:" != thefield(' active -afbeelding2')

您应该使用。在the_field('actietitel')之后,而不是;。此外,您可能会忘记调用echo。试试这个

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

Try

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

这将,基本上,测试isset(the_field('actie-afbeelding2'))!the_field('actie-afbeelding2'),其中包括is_null(the_field('actie-afbeelding2'))

这篇文章有一个很好的表格总结。