0

In WordPress I'm using a plug in that logs a meta key _featured and adds a value of yes or no. I want to add css if it is featured, however it is adding the div no matter what the result.

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

Not all of this is intended for the end, some of it is just to test the results of the log.

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

This checks if there is a value. Works fine.

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

Logging it as a variable

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

                                <?php endif; ?>

This is the code to add the div. It adds it regardless of whether the value is yes or no.

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

This last portion is simply to check what the value is for myself.

I'm not sure where I am going wrong.

5
  • I personally dislike strcasecmp. I would use strtolower($feat) == 'yes' for my comparison Commented Oct 19, 2013 at 3:13
  • @cale_b Yeahhh I' having massive issues with this string comparison stuff. I have two string comparisons on the page and both are broken as all hell. I tried $feat === 'yes' and strcmp($feat, 'yes') neither works. Commented Oct 19, 2013 at 3:16
  • So what does var_dump($feat) output? You should use var_dump instead of echo to see the contents of a variable - it gives you the type of variable and other useful information. Commented Oct 19, 2013 at 3:17
  • It outputs yes or no depending on the status. And thank you for the tip. There has to be something wrong with my if argument, I changed the variable to a string length and then changed the if argument to >2. Still prints each time. Commented Oct 19, 2013 at 3:25
  • in your first code paste there are no single quotes around yes, can you confirm it is always in single quotes. Can you paste the output of var_dump($feat) here? Commented Oct 19, 2013 at 3:27

2 Answers 2

2

Your HTML isn't wrapped in PHP and thus isn't affected by the conditional statement

Change

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

                                <?php endif; ?>

to

<?php 
  if(strcasecmp($feat, 'yes') == 0){
       echo "<a href = ''><div class = 'featured_reject'>Featured Rejection</div></a>"
  }
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Or my preference is to actually put the : at the end of the of statement (or use {} for wrapping)
This was the correct answer. For some reason I thought it would treat it the ?php if and endif as a wrapper and execute the code between that if it was true. Thank you.
@PatrickCauley - it will. You understood it right. I suspect it's the lack of curly brace or colon at the end of the if line.
1

The syntax of the php if..endif is:

if (condition):
   ...
endif;

(per: http://php.net/manual/en/control-structures.alternative-syntax.php )

So you need to change

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

to (note the extra : after ==) in the if statement:

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

1 Comment

and I just saw the comments from jdog to the answer by Lloyd Banks. I think my only reaction is going to be "what he said..."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.