RUSSEL86

taxonomy/Category émergency code

Feb 16th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.32 KB | None | 0 0
  1. //current page taxonomy name and info
  2. ----------------
  3. $termss = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );echo $termss->name;
  4.  
  5. // query for only parent category info with ACF category metafield
  6. ---------------------------
  7. $args = array( 'hide_empty=0' );
  8. $terms = get_terms( 'product_cat', array( 'parent' => 0 ),$args );
  9. if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
  10.    foreach ( $terms as $term ) {
  11.     $category_name = $term->name;
  12.       $category_description = $term->description;
  13.       $category_link = esc_url( get_term_link( $term ) );
  14.       $category_banner = get_field( "category_banner", $term );
  15.  
  16.         //content
  17.         echo '<a href="'.$category_link.'">'.$category_name.'</a>';
  18. }
  19. }
  20.  
  21. // example for child taxonomy
  22. ------------------------------------
  23. <?php
  24. $term_id = 10;
  25. $taxonomy_name = 'products';
  26. $termchildren = get_term_children( $term_id, $taxonomy_name );
  27.  
  28. echo '<ul>';
  29. foreach ( $termchildren as $child ) {
  30.     $term = get_term_by( 'id', $child, $taxonomy_name );
  31.     echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
  32. }
  33. echo '</ul>';
  34. ?>
  35.  
  36. // category query example this is for taxonomy.php or taxonomy-taxname.php
  37. -------------------------------------------------------------------------------
  38. <?php
  39. $termss = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // to get current category
  40. $terms = get_terms( 'product_cat', $termss );
  41.  if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
  42.      
  43.      foreach ( $terms as $term ) {
  44.       $category_name = $term->name;
  45.       $category_description = $term->description;
  46.       $category_link = esc_url( get_term_link( $term ) );
  47.       $category_banner = get_field( "category_banner", $term );
  48.       $category_page_logo = get_field( "category_page_logo", $term );
  49.       $category_image = get_field( "category_image", $term );
  50.       $category_color = get_field( "category_color", $term );
  51.      
  52.       ?>
  53.  <style type="text/css">
  54. .pdpage_items_cattitle h2 {color:<?php echo $category_color; ?>}
  55. .pdpage_items_cattitle h2:before{content:"";background: <?php echo $category_color; ?>;}
  56. .pdpage_items_cattitle h2:after{content:"";background: <?php echo $category_color; ?>;}
  57.  
  58.  </style>
  59.  
  60. <section class="allpage_banner_main_area fix withpditem" style="background:url(<?php echo $category_banner; ?>) no-repeat; background-size: cover;">
  61.     <div class="container">
  62.         <div class="row">
  63.             <div class="col-md-12">
  64.                 <div class="allpage_banner_title_wrapper fix">
  65.                     <div class="allpage_banner_title_area fix">
  66.                         <div class="col-md-5 text-left">
  67.                             <div class="allpage_banner_title">
  68.                                 <h1><?php _e("<!--:en-->Product<!--:--><!--:pb-->Produtos<!--:-->"); ?></h1>
  69.                             </div>
  70.                         </div>
  71.                         <div class="col-md-5 col-md-offset-2 text-right">
  72.                             <div class="allpage_banner_breadcumb">
  73.                                 <p><?php my_breadcrumb(); ?></p>
  74.                             </div>
  75.                         </div>
  76.                     </div>
  77.                 </div>
  78.             </div>
  79.         </div>
  80.     </div>
  81. </section>
  82.  
  83. <section class="pdpage_main_content_area fix">
  84.     <div class="container">
  85.         <div class="row">
  86.             <div class="col-md-12">
  87.                 <div class="pdpage_main_content fix">
  88.                     <div class="pdpage_titleitem_content fix"  style="background-color:<?php echo $category_color; ?>">
  89.                         <div class="col-md-3 col-sm-4 margi_left_zero">
  90.                             <div class="pdpage_sample_img">
  91.                                 <img src="<?php echo $category_image;?>" alt="" />
  92.                             </div>
  93.                         </div>
  94.                         <div class="col-md-9 col-sm-8">
  95.                             <div class="product_catmain_logo fix">
  96.                                 <img src="<?php echo $category_page_logo; ?>" alt="" />
  97.                             </div>
  98.                             <div class="product_catmain_descrip fix">
  99.                                 <p><?php echo $category_description; ?></p>
  100.                             </div>
  101.                         </div>
  102.                     </div>
  103.                
  104.                  <?php
  105.                  // tax query for child tax items
  106. $term_id = $termss->term_id;
  107. $taxonomy_name = 'product_cat';
  108. $termchildren = get_term_children( $term_id, $taxonomy_name );
  109. foreach ( $termchildren as $child ) {
  110.     $term = get_term_by( 'id', $child, $taxonomy_name );
  111.     $tag_id = $term->term_id;
  112.                     ?><div class="pdpage_items_content fix row">
  113.                         <div class="col-md-12">
  114.                             <div class="pdpage_items_cattitle">
  115.                                 <h2><?php echo $term->name ; ?></h2>
  116.                             </div>
  117.                         </div>
  118.                         <div class="pdpage_catitems">
  119.                    
  120.                         <?php
  121.                         $tax_child= new WP_Query(array(
  122.                             'post_type' => 'product-items',
  123.                             'posts_per_page' => -1,
  124.                             'tax_query' => array(
  125.                                                 array(
  126.                                                     'taxonomy' => 'product_cat',
  127.                                                     'field'    => 'term_id',
  128.                                                     'terms'    => $tag_id,
  129.                                                 ),
  130.                                             ),
  131.                         ));
  132.                     if($tax_child -> have_posts()):
  133.                     while($tax_child -> have_posts()): $tax_child -> the_post();?>
  134.                        
  135.                             <div class="col-md-6 col-sm-6">
  136.                                 <div class="pdpage_catitem_single">
  137.                                     <div class="row">
  138.                                         <div class="col-md-3 col-sm-3">
  139.                                             <div class="image_catitem_single" style="background-color:<?php echo $category_color; ?>">
  140.                                                 <img src="<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), '' ); echo $image[0]; ?>" alt="" />
  141.                                             </div>
  142.                                         </div>
  143.                                         <div class="col-md-9 col-sm-9">
  144.                                             <div class="catitem_single_dtails">
  145.                                                 <div class="catitem_single_title" style="color:<?php echo $category_color; ?>">
  146.                                                     <h2><?php the_title();?> </h2>
  147.                                                     <h4><?php the_field('product_subtitle');?></h4>
  148.                                                 </div>
  149.                                                 <div class="catitem_single_textexcerpt">
  150.                                                     <?php the_content();?>
  151.                                                 </div>
  152.                                                 <div class="catitem_single_popbtnarea">
  153.                                                     <div class="row">
  154.                                                         <div class="col-md-6 col-sm-6">
  155.                                                             <a href="#request_pop" class="btn_request prodtform_pop">solicite uma amostra</a>
  156.                                                         </div>
  157.                                                         <div class="col-md-6 col-sm-6">
  158.                                                             <a href="#morinfo_pop" class="btn_moreinfo prodtform_pop">MAIS INFORMAÇÕES</a>
  159.                                                         </div>
  160.                                                     </div>
  161.                                                 </div>
  162.                                             </div>
  163.                                         </div>
  164.                                     </div>
  165.                                 </div>
  166.                             </div>
  167.                             <?php endwhile; ?> <?php endif; wp_reset_query();?>
  168.                            
  169.                         </div>
  170.                     </div> <?php
  171.                    
  172.                     }?>
  173.                                   <?php
  174.        
  175.      }
  176.  
  177.  } ?>
  178.                    
  179.                 </div>
  180.             </div>
  181.         </div>
  182.     </div>
  183. </section>
Advertisement
Add Comment
Please, Sign In to add comment