在PHP Array中找到一个类别

时间:2015-11-27 14:04:27

标签: php arrays wordpress

这是我的代码

<?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> ';
?>

这显示了$ portfolio_categories中类别名称创建的过滤器 然后,创建了客户列表

<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>   

我想要做的是为每个不同的类别显示不同的元素。 我想在标签之前插入一个if条件,比如这个

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

感谢所有的时间, 丹

1 个答案:

答案 0 :(得分:0)

您需要存储最后一个类别,否则每行之前都会有if ($res["acknowledgement"] == 'false') 个标记:

<figure>