将 php 与 html 标签混合在一起


Mixing php with html tags

我正在尝试实现以下代码,但结果没有显示任何内容。

 <li class="cleanup">Garantie:<span><?php (get_post_meta(get_the_ID(), 'Garantie', true); ?> Jahre</span><?php echo (get_post_meta(get_the_ID(), 'Garantie', true) >= 2) ? ('<span class="pro_con pro"><i class="fa fa-check"></i>Lange Garantie: ' . <?php (get_post_meta(get_the_ID(), 'Garantie', true)); ?> . ' Jahre</span>') : ('<span class="pro_con pro"><i class="fa fa-check"></i>Standart Garantie: ' . <?php (get_post_meta(get_the_ID(), 'Garantie', true)); ?> . ' Jahre</span>'; ?>) </li>

该字段应该可用,并且get_post_meta功能确实有效!

假设我有一个语法错误,但我不确定在哪里?

有什么建议我做错了什么吗?

感谢您的回复!

您有一个额外的括号,更改为

<?php get_post_meta(get_the_ID(), 'Garantie', true); ?>
//   ^ it was here

顺便说一下,您必须在devel env上登录,日志有很大帮助。

在三元运算符中有一些额外的结束?> php 标签。

修改后的代码:

 <li class="cleanup">Garantie:
 <span>
 <?php get_post_meta(get_the_ID(), 'Garantie', true); ?> Jahre
 </span>
 <?php 
 echo (get_post_meta(get_the_ID(), 'Garantie', true) >= 2) ? 
 ('<span class="pro_con pro"> <i class="fa fa-check"></i>Lange Garantie: ' . 
 (get_post_meta(get_the_ID(), 'Garantie', true)) . ' Jahre</span>') : 
 ('<span class="pro_con pro"> <i class="fa fa-check"></i>Standart Garantie: ' . 
    (get_post_meta(get_the_ID(), 'Garantie', true)) . ' Jahre</span>' ) ;
?>
 </li>

试试这个:

    <li class="cleanup">Garantie:<span><?php (get_post_meta(get_the_ID(), 'Garantie', true); ?> Jahre</span>
<?php echo (get_post_meta(get_the_ID(), 'Garantie', true) >= 2) ? '<span class="pro_con pro"><i class="fa fa-check"></i>Lange Garantie: ' . get_post_meta(get_the_ID(), 'Garantie', true) . ' Jahre</span>' : '<span class="pro_con pro"><i class="fa fa-check"></i>Standart Garantie: ' . get_post_meta(get_the_ID(), 'Garantie', true) . ' Jahre</span>'; ?> </li>

像这样的事情怎么样:

 <li class="cleanup">
    Garantie:
    <span>
        <?php echo get_post_meta(get_the_ID(), 'Garantie', true); ?> Jahre
    </span>
    <?php 
        if(get_post_meta(get_the_ID(), 'Garantie', true) >= 2){
            echo '<span class="pro_con pro"><i class="fa fa-check"></i>Lange Garantie: ';
            echo get_post_meta(get_the_ID(), 'Garantie', true);
        }else{
            echo '<span class="pro_con pro"><i class="fa fa-check"></i>Standart Garantie: ';
            echo get_post_meta(get_the_ID(), 'Garantie', true);
        }
        echo " Jahre</span>";
    ?>
</li>

毕竟,可读性很重要...