RUSSEL86

all about category , taxonomy , term

Jan 13th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.23 KB | None | 0 0
  1. //This code for category
  2. ------------------------
  3. <?php the_category(', '); ?>
  4. <?php wp_dropdown_categories('class=cat_select'); ?>
  5. <?php the_taxonomies(array('template' => '% %l')); ?>
  6. <?php echo get_the_term_list( $post->ID, 'project_cat', '', '', '' );?>
  7.  
  8. //get parent taxonomy id from taxonomy id child
  9. ------------------------------------
  10. $subCategory = get_term_by( 'id', $search_region, 'houseboats_cat' );
  11. $mainCategory = get_term_by( 'id', $subCategory->parent, 'houseboats_cat' );
  12. $parent_cat = $mainCategory->term_id;  
  13.  
  14. // get taxonomy terms for parent and child separately
  15. -------------------------------
  16. foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) {
  17.   // display top level term name
  18.   echo $parent_term->name . '<br>';
  19.  
  20.   foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term ) {
  21.     // display name of all childs of the parent term
  22.     echo $child_term->name . '<br>';
  23.   }
  24.  
  25. }
  26.  
  27. // category feature image ACF
  28. ---------------------------
  29. <?php
  30. $term = get_queried_object();
  31. $image = get_field('category_featured_image', $term);
  32.  
  33. if( $image ): ?>
  34. <div class="single_blog_thumb fix">
  35. <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['title']; ?>" />
  36. <div class="page_title_area fix">
  37. <h1><?php single_cat_title(); ?></h1>
  38. </div>
  39. </div>
  40. <?php else : ?>
  41. <div class="defaltpage_title_area fix">
  42. <h1><?php single_cat_title(); ?></h1>
  43. </div>
  44. <?php endif; ?>
  45.  
  46. //This code for category without link
  47. --------------------------------------
  48. <?php $terms_as_text = get_the_term_list( $post->ID, 'product_specialties', '', ', ', '' ) ; echo strip_tags($terms_as_text); ?>
  49.  
  50. //This code for getting taxonomy with ul>li>a
  51. ----------------------------------
  52. <?php
  53. $taxonomy = 'project_cat';
  54. $terms = get_terms('project_cat');
  55. ?>
  56. <ul id="home_links" class="fix">
  57. <?php
  58. foreach ($terms as $term) {
  59. $term_link = get_term_link( $term, 'project_cat' );
  60.         if( is_wp_error( $term_link ) )
  61.             continue;
  62. echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
  63. }
  64. ?>
  65. </ul>
  66.  
  67. //This code for Taxonomy query
  68. ----------------------------------
  69. <?php $terms = get_terms( array(
  70. 'taxonomy' => 'representante_cat',
  71. 'hide_empty' => true,
  72. ) );
  73. if ( !empty( $terms ) && !is_wp_error( $terms ) ){
  74. foreach ( $terms as $term ) { ?>
  75. <option value="<?php echo $term->term_id; ?>" ><?php echo $term->slug; ?></option>
  76. <?php }
  77. } ?>
  78.  
  79. //This code for Child Taxonomy query without parent name with alphabetic title order
  80. ------------------------------------------------------------------------------
  81. $taxonomy_name = 'houseboats_cat';
  82. $parent_terms = get_terms( $taxonomy_name, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );  
  83. echo '<ul>';
  84. foreach ( $parent_terms as $pterm ) {
  85.     //Get the Child terms
  86.     $terms = get_terms( $taxonomy_name, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
  87.     foreach ( $terms as $term ) {
  88.         echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';  
  89.     }
  90. }
  91. echo '</ul>';
  92.  
  93. How to order get_term_children output by alphabetic order
  94. --------------------------------------------------------
  95. <div class="get_term_child by_alphabetic_order">
  96. <?php
  97. $term_id = 20;
  98. $taxonomy_name = 'meeting_type';
  99. $termchildren = get_term_children( $term_id, $taxonomy_name );
  100. $children = array();
  101. foreach ($termchildren as $child) {
  102.   $term = get_term_by( 'id', $child, $taxonomy_name );
  103.   $children[$term->name] = $term;
  104. }
  105. ksort($children);?>
  106. <!-- now $children is alphabetized -->
  107. <select name='eventcats' id='eventcats'   onchange='taclinkch(this.id)'>
  108.   <option value=''>Select a Topic</option>
  109.   <?php
  110.   foreach ( $children as $child ) {
  111.     $term = get_term_by( 'id', $child->term_id, $taxonomy_name );
  112.     echo '  <option value="' . $term->term_id . '">' . $term->name . '</option>';
  113.   } ?>
  114. </select>
  115. </div>
  116.  
  117. // taxonomy query with pagination
  118. ------------------------------------------
  119. <div class="produtos_filter_result fix">
  120. <?php
  121. $taxonomy = 'produtos_cat';
  122. $number   = 6; // number of terms to display per page
  123. // Setup:
  124. $page         = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
  125. $offset       = ( $page > 0 ) ?  $number * ( $page - 1 ) : 1;
  126. $totalterms   = wp_count_terms( $taxonomy, array( 'hide_empty' => TRUE ) );
  127. $totalpages   = ceil( $totalterms / $number );
  128. $args = array(
  129. 'orderby'       => 'name',
  130. 'order'         => 'ASC',
  131. 'hide_empty'    => true,
  132. 'number'        => $number,
  133. 'offset'        => $offset,
  134. );
  135. $terms = get_terms( $taxonomy, $args );
  136. foreach ( $terms as $term ){
  137. $term_link = get_term_link( $term );
  138. ?>
  139. <div class="cat_filter_result_item fix text-center">
  140. <div class="cat_filter_result_thumb fix">
  141. <img src="<?php the_field('catefory_photo_thumb', $term); ?>" alt="">
  142. </div>
  143. <div class="cat_filter_result_iconcat fix">
  144. <a href="<?php echo esc_url( $term_link ) ?>"><img src="<?php the_field('category_icon', $term); ?>" alt=""></a>
  145. <div class="filter_cat_title fix">
  146. <a href="<?php echo esc_url( $term_link ) ?>"><h5><?php echo $term->name; ?></h5></a>
  147. </div>
  148. </div>
  149. <div class="cat_filter_result_title fix">
  150. <a href="<?php echo esc_url( $term_link ) ?>"><h4>ConheƧa Mais</h4></a>
  151. </div>
  152. </div>
  153. <?php }
  154. ?>
  155. </div>
  156. <div class="produtos_paginationresult fix">
  157. <div class="mypagination_area text-center">
  158. <?php
  159. printf( '<nav class="pagination">%s</nav>',
  160. custom_page_navi( $totalpages, $page, 6, 0 )
  161. );
  162.  
  163. ?>
  164. </div>
  165. </div>
  166.  
  167. // this code for function.php
  168. function custom_page_navi( $totalpages, $page, $end_size, $mid_size )
  169. {
  170.     $bignum = 999999999;
  171.  
  172.     if ( $totalpages <= 1 || $page > $totalpages ) return;
  173.  
  174.     return paginate_links( array(
  175.         'base'          => str_replace( $bignum, '%#%', esc_url( get_pagenum_link( $bignum ) ) ),
  176.         'format'        => '',
  177.         'current'       => max( 1, $page ),
  178.         'total'         => $totalpages,
  179.         'prev_text'     => '<',
  180.         'next_text'     => '>',
  181.         'type'          => 'list',
  182.         'show_all'      => false,
  183.         'end_size'      => $end_size,
  184.         'mid_size'      => $mid_size
  185.     ) );
  186. }
  187.  
  188. // select filter area caegory and jquery for get url and redirect to taxonomy page for query select option
  189. -----------------------------------------------------------------------------
  190. <form action="" id="cat_dorar_form" method="get">
  191. <div class="filter_choose_cat_area fix">
  192. <select name="cat_select" id="category-select" class="filter_catgory">
  193. <option value="">Selecione uma categoria</option>
  194. <?php
  195. $product_cats = get_terms( array(
  196. 'taxonomy' => 'produtos_cat',
  197. 'hide_empty' => true,
  198. ) );
  199. if ( ! empty( $product_cats ) && ! is_wp_error( $product_cats ) ){
  200. foreach ( $product_cats as $product_cat ) {
  201. if(!empty($_GET['cat_select'])){
  202. $product_id = $_GET['cat_select'];
  203. if($product_id == $product_cat->slug){
  204. $product_return = " selected='selected'";
  205. } else{
  206. $product_return = "";
  207. }
  208. }
  209. echo '<option value="'.$product_cat->slug.'" '.$product_return.' data-id="'.$product_cat->term_id.'">'.$product_cat->name.'</option>';
  210. }
  211. }
  212. ?>
  213. </select>
  214. </div>
  215. <div class="filter_submit fix">
  216. <input type="submit" value="Filtrar" name="pro-filt" class="filter_submitbtn" />
  217. </div>
  218. </form>
  219. // use this js
  220. --------
  221. jQuery(document).ready(function(){
  222. var linkase = '<?php echo site_url();?>/produtos-category/';
  223. var linslug;
  224. jQuery('select#category-select').change(function() {
  225. linslug = jQuery(this).val();
  226. });
  227. jQuery("#cat_dorar_form").submit(function(e) {
  228. e.preventDefault();
  229. var jakhusi = linkase+linslug;
  230. window.location.replace(jakhusi);
  231. });
  232. });
  233. // or use this js its take cat id and name to url
  234. ---------------------------------------
  235. jQuery(document).ready(function(){
  236. var linkase = '<?php echo site_url();?>/produtos-category/';
  237. var linslug, linkID;
  238. jQuery('select#category-select').change(function() {
  239. linslug = jQuery(this).val();
  240. linkID = jQuery('option:selected',this).attr('data-id');
  241. });
  242. jQuery("#cat_dorar_form").submit(function(e) {
  243. e.preventDefault();
  244. var jakhusi = linkase+linslug+'/?cat_select='+linkID+'&pro-filt=Filtrar';
  245. //console.log(jakhusi);
  246. window.location.replace(jakhusi);
  247. });
  248. });
Advertisement
Add Comment
Please, Sign In to add comment