dongping1689 2015-11-27 14:04
浏览 40

在PHP Array中找到一个类别

this is my code

<?php 
    $portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
    foreach($portfolio_categories as $portfolio_category)
        echo '<li data-filter=".' .$portfolio_category->slug. '"><a href="#">' .$portfolio_category->name. '</a></li> ';
?>

this shows the filters created by the category's name inside $portfolio_categories then, there is the creation of client's list

<ul id="list" class="portfolio_list clearfix responsive">                   
    <?php
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby'=> 'title', 'order' => 'ASC' ) );
    ?>

    <?php if (have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <?php $terms = wp_get_object_terms($post->ID, 'portfolio_category'); ?>
        <?php $pf_bimg = wp_get_attachment_url( get_post_thumbnail_id() );?>
        <?php $pf_simg = aq_resize( $pf_bimg, 420, 450, true ); ?>                          
        <li class="span3 list_item <?php foreach ($terms as $term) { echo $term->slug.' '; } ?>">
            <div class="recent-item">
                <figure>
                    <div class="touching medium">
                        <a href="<?php the_permalink(); ?>" class=""><img src="<?php echo $pf_simg; ?>" alt="<?php the_title(); ?>" /></a>
                        <!-- <a href="<?php echo $pf_bimg; ?>" class="hover-zoom mfp-image"><i class="icon-search"></i></a>
                        <a href="<?php the_permalink(); ?>" class="hover-link"><i class="icon-link"></i></a> -->
                    </div>
                    <figcaption class="item-description">
                        <h5><a href="<?php the_permalink(); ?>" class=""><?php the_title(); ?></a></h5>
                        <span><?php $i = 0; foreach ($terms as $term) { if($i)echo " / "; echo $term->name; $i=1; } ?></span>
                    </figcaption>
                </figure>
            </div>
        </li>
    <?php endwhile; ?> <?php endif; ?>
    <?php wp_reset_query();?>                            
</ul>   

What i 'm trying to do is to display a different element for every different category . I thought to insert an if condition before the tag , like this

<?php 
    $portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
    if ($portfolio_category = 'category1'): ?>
         <figure>1</figure>
    <?php else: ?>
         <figure>2</figure>
    <?php endif 
?>

Thank all for your time, Dan

  • 写回答

1条回答 默认 最新

  • douchen3562 2015-11-27 14:15
    关注

    You need to store last category, otherwise you'll have <figure> tag before each row:

    <?php
        $portfolio_categories = get_categories(array('taxonomy'=>'portfolio_category'));
    ?>
    <?php if ($portfolio_category != $prev_portfolio_category): ?>
        <?php if ($portfolio_category = 'category1'): ?>
            <figure>1</figure>
        <?php else: ?>
            <figure>2</figure>
        <?php endif; ?>
    <?php endif; ?>
    <?php 
        $prev_portfolio_category = $portfolio_category;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c