【问题标题】:Variable Scope Wordpress Query可变范围 Wordpress 查询
【发布时间】:2017-05-19 09:50:41
【问题描述】:

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

如何从我的if 语句中访问$project_cats

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

【问题讨论】:

    标签: php wordpress scope


    【解决方案1】:

    我刚刚意识到发生了什么,在 if 语句中,类别名称附加了一个空格 ' ',因此我的 if 语句从来都不是 TRUE。固定的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 2019-05-20
      • 2012-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多