2015-11-27 72 views
1

這是我的代碼找到PHP數組內的類別

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

感謝大家的時間, 丹

回答

0

您需要存儲最後一類,否則你會每行前有<figure>標籤:

<?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; 
?> 
+0

你好mbrzuchalski,感謝您的快速答案; 我編輯了代碼,以這種方式,結果是僅第一個數組的元素 –