0

在 WordPress 中,我使用了一个插件,它记录一个元键 _featured 并添加一个值是或否。如果有特色,我想添加 css,但是无论结果如何,它都会添加 div。

            <?php if ( get_post_meta( get_the_ID(), '_featured', true ) ) : ?>
                <?php $feat = get_post_meta( get_the_ID(), '_featured', true ); ?>
                            <?php if( strcasecmp($feat, yes) == 0)?>
                                <a href=""><div class="featured_reject">Featured Rejection</div></a>

                            <?php endif; ?>
                            <h1><?php echo get_post_meta( get_the_ID(), '_featured', true ) ?></h1>
                <?php endif; ?>

并非所有这些都是为了结束,其中一些只是为了测试日志的结果。

            <?php if ( get_post_meta( get_the_ID(), '_featured', true ) ) : ?>

这会检查是否有值。工作正常。

<?php $feat = get_post_meta( get_the_ID(), '_featured', true ); ?>

将其记录为变量

<?php if( strcasecmp($feat, 'yes') == 0)?>
                                    <a href=""><div class="featured_reject">Featured Rejection</div></a>

                                <?php endif; ?>

这是添加 div 的代码。无论值是或否,它都会添加它。

<h1><?php echo get_post_meta( get_the_ID(), '_featured', true ) ?></h1>
                    <?php endif; ?>

最后一部分只是为了检查我自己的价值。

我不确定我哪里出错了。

4

2 回答 2

2

您的 HTML 未包含在 PHP 中,因此不受条件语句的影响

改变

<?php if( strcasecmp($feat, 'yes') == 0)?>
                                    <a href=""><div class="featured_reject">Featured Rejection</div></a>

                                <?php endif; ?>

<?php 
  if(strcasecmp($feat, 'yes') == 0){
       echo "<a href = ''><div class = 'featured_reject'>Featured Rejection</div></a>"
  }
?>
于 2013-10-19T03:27:19.090 回答
1

php if..endif 的语法是:

if (condition):
   ...
endif;

(根据: http: //php.net/manual/en/control-structures.alternative-syntax.php

所以你需要改变

<?php if( strcasecmp($feat, yes) == 0)?>
    <a href=""><div class="featured_reject">Featured Rejection</div></a>
<?php endif; ?>

在 if 语句中(注意额外的 : after ==):

<?php if( strcasecmp($feat, yes) == 0):?>
    <a href=""><div class="featured_reject">Featured Rejection</div></a>
<?php endif; ?>
于 2013-10-19T05:06:33.347 回答