Notice → Undefined variable

<?php If($layout_one == 'env_class') {$lay_class = 'env_class';} ?>
<div id="post-<?php the_ID(); ?>" <?php post_class($lay_class);?>>

The above is generating a PHP error →

Notice: Undefined variable: lay_class

What is the fix for this?

$lay_class needs to be defined.
In your script it is only defined under a certain condition.
You could just set it to an empty string before the condition, assuming your intention is to add a class attribute to the div only if the condition is met.

1 Like

How can you please guide me?

<?php 
$lay_class = '' ; // An empty string
If($layout_one == 'env_class') {$lay_class = 'env_class';} 
?>
<div id="post-<?php the_ID(); ?>" <?php post_class($lay_class);?>>

Though I don’t know how your post_class() function works. Will it return false and do nothing if given an empty string?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.