可变范围Wordpress查询

时间:2017-05-19 09:50:41

标签: php wordpress scope

我正在尝试根据包含类别名称的名为$project_cats的变量的内容来更改CSS类。虽然$project_cats似乎在我的整个脚本中都有效,但它在我的if个语句中无效,至少这是我的假设。

如何从$project_cats声明中访问if

<?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.";
}
?>

1 个答案:

答案 0 :(得分:0)

我刚刚意识到发生了什么,在if语句中,该类别的名称有空格&#39; &#39;附加到它,因此我的if语句永远不会为TRUE。固定。