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

PHP mixed with html mixed with wordpress tags gets wrong output

这个问题非常具体,对于你们中的一些人来说可能真的很容易回答。我已经为客户的网站苦苦挣扎了 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>"
                                                           ?> 

但由于某种原因它不会工作(这是我的数百次尝试,如果这部分代码真的很糟糕,我很抱歉)。实际上,它有效,但它 return 是 URL 而不是在灯箱中。但是我向您展示的第一段代码实际上 return 灯箱中的 link。

您可以在以下位置自行查看:http://www.bgc-testomgeving.nl/jorn/ambachtelijk/

希望大家帮帮我!

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

也许问题是

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

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

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

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

使用此代码

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

尝试

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

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

this article 总结的很好table。