0

I have a problem with defining a wordpress variable for a class. This is my code: I created a custom field for posts with the anme class. Here I'd like to retrieve the class. If the custom field is empty, the class should be set to "col1" by default

$masonry_col_class = get_post_meta($post->ID, 'class', true);
if (!empty($masonry_col_class)){ $masonry_col_class='col1'; };

And here's the output for the css:

<ul class="<?php echo $masonry_col_class ?>">

The if (!empty...) doesn't seem to work though.

I would be thankful for any help. -n-

1 Answer 1

2

You have actually written : if the variable I have just defined is NOT empty, then set it to "col1". You should do exactly the opposite : if the variable IS empty, like this :

$masonry_col_class = get_post_meta($post->ID, 'class', true);
if (empty($masonry_col_class)){ $masonry_col_class='col1'; };

(as I guess your idea is : either the class is defined in the custom field and we have it here, or else I use the generic "col1" class... is that correct?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.