I am trying to change a CSS class, based on the contents of a variable called $project_cats which contains a category name. While $project_cats appears to be valid throughout my script, it is not valid within one of my if statements, at least that is my assumption.

How can I access $project_cats from within my if statement?

<?php

if ( $query->have_posts() )
{
    ?>
    <div id="myresource">
        <?php

                while ($query->have_posts())
                {
                    $query->the_post();

                    ?>

                    <?php 
                        $terms = get_the_terms($post->id,"project-type");
                        $project_cats = NULL;

                            if ( !empty($terms) ){
                                foreach ( $terms as $term ) {
                                    $project_cats .= strtolower($term->name) . ' ';
                                }
                            }
                            //$project_cats does not appear to have content in the below if statement
                            if ($project_cats == "webinars") {
                                $iconclass = "iconsmind-Video-4";
                            }

                                ?>

                        <div class="nectar-icon-list" data-icon-color="default" data-icon-style="border" data-icon-size="small" data-animate="">
                            <div class="nectar-icon-list-item">
                            <div class="list-icon-holder" data-icon_type="icon" style="background-color:#1963af;">
                                <i class="icon-default-style <?php echo $iconclass; ?>" data-color="default"></i>
                            </div>
                            <div class="content">
                                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                <?php the_excerpt(); ?>
                                <?php echo $project_cats;?>
                            </div>
                            </div>
                        </div>


                    <?php
                }

        ?>
    </div>

    <?php
}
else
{
    echo "No Results Found.";
}
?>

Read more here: Variable Scope WordPress Query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: