【问题标题】:wordpress - how to exclude category from foreach loopwordpress - 如何从 foreach 循环中排除类别
【发布时间】:2013-09-23 12:41:19
【问题描述】:

我的 wordpress theme-functions.php 文件中有这个过滤类别功能:

function tie_categories_filter() {
    if( tie_get_option( 'enable_filter' ) && tie_get_option( 'on_home' ) == 'grid' ):
        $exc_home_cats = tie_get_option( 'exc_home_cats' );
        if( $exc_home_cats )
            $comma_cats_separated = @implode(",", $exc_home_cats );
        $categories = get_categories('exclude='.$comma_cats_separated); ?>

<ul id="filters">
  <li class="current all-items"><a href="#" data-filter="*"><?php _e( 'All' , 'tie' ) ?></a></li>
  <li class="current all-items"><a href="#" data-filter=".cat_1">General</a></li>

<?php
  foreach($categories as $category) { ?>
    <li><a href="#" data-filter=".cat_<?php echo $category->term_id ?>"><?php echo $category->name ?></a></li>
<?php } ?>
</ul>
<?php
    endif;
}

我添加了这一行:

<li class="current all-items"><a href="#" data-filter=".cat_1">General</a></li>

所以在站点中它显示“全部”过滤器,然后是“常规”,然后我想显示其余类别。

所以我需要从循环中排除“常规”类别。 我可以通过 wordpress term_id=1 联系到她,

我如何在 php 中做到这一点?

谢谢。

【问题讨论】:

    标签: php wordpress foreach categories


    【解决方案1】:

    最简单的选择是在循环中添加if 语句以检查term_id 是否为1。如果是,则continue 循环。

    <?php
    foreach ($categories as $category) {
        if ($category->term_id === 1) {
            continue;
        }
    ?>
        <li><a href="#" data-filter=".cat_<?php echo $category->term_id ?>"><?php echo $category->name ?></a></li>
    <?php
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多