如何在元标记有值时添加类

时间:2016-02-16 01:33:03

标签: php html wordpress

我正在尝试编写代码,如果我编写的自定义元框具有值,那么它应该将类添加到div或按钮。但我不确定它是否有效,因为Dreamweaver说代码不正确或者其他东西......

这是我的代码

<?php
    $offers = get_post_meta( get_the_ID(), 'couponshortcode', true );
    if( $offers ) {
        echo "<button class="filter" data-filter=".couponclass"><i class="fa fa-tags"></i></button>";
    } 
?>

1 个答案:

答案 0 :(得分:2)

您需要在此处使用单引号:

<?php 
$offers = get_post_meta( get_the_ID(), 'couponshortcode', true ); 
if( $offers ) { 
echo '
<button class="filter" data-filter=".couponclass">
<i class="fa fa-tags"></i>
</button>
'; 
} 
?>

第二个解决方案:

<?php 
$offers = get_post_meta( get_the_ID(), 'couponshortcode', true ); 
if( $offers ) { 
?>
     <button class="filter" data-filter=".couponclass">
     <i class="fa fa-tags"></i>
     </button>
<?php
 } 
 ?>

我不确定这个点 data-filter=".couponclass"。也许它不是必需的。