kisabelle
1/30/2014 - 5:34 PM

Wordpress Terms and Taxonomies

Wordpress Terms and Taxonomies / Page Categories

<?php

  // ---------------------------------------
  // Add categories and tags to Pages
  // ---------------------------------------

  function add_taxonomies_to_pages() {
    register_taxonomy_for_object_type( 'post_tag', 'page' );
    register_taxonomy_for_object_type( 'category', 'page' );
  }
  add_action( 'init', 'add_taxonomies_to_pages' );

  // ---------------------------------------
  // Get category top-level parent
  // ---------------------------------------
  
    $parent  = get_term_by( 'id', $category_ID, 'product_cat');
    while ($parent->parent != 0){
      $parent  = get_term_by( 'id', $parent->parent, 'product_cat');
    }

  // ---------------------------------------
  // Check if a term has no parent
  // ---------------------------------------
  
  $terms = get_terms('product_cat');
  foreach ($terms as $term) {
    if($term->parent == 0 ){
      echo $term->name;
    }
  }
  
  // ---------------------------------------
  // Check if a term has parent or children
  // ---------------------------------------
  
  $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
  $parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
  $children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
  
  if(($parent->term_id!="" && sizeof($children)>0)) {
  	// has parent and child
  }elseif(($parent->term_id!="") && (sizeof($children)==0)) {
  	// has parent, no child
  }elseif(($parent->term_id=="") && (sizeof($children)>0)) {
  	// no parent, has child
  }
  
  // ----------------------------------------------------
  // Check if a post is in/tagged with a custom taxonomy
  // ----------------------------------------------------
    
  /* Function */
  
  /**
  * Conditional function to check if post belongs to term in a custom taxonomy.
  *
  * @param    tax        string                taxonomy to which the term belons
  * @param    term    int|string|array    attributes of shortcode
  * @param    _post    int                    post id to be checked
  * @return             BOOL                True if term is matched, false otherwise
  */
  function pa_in_taxonomy($tax, $term, $_post = NULL) {
      // if neither tax nor term are specified, return false
      if ( !$tax || !$term ) { return FALSE; }
      // if post parameter is given, get it, otherwise use $GLOBALS to get post
      if ( $_post ) {
          $_post = get_post( $_post );
      } else {
          $_post =& $GLOBALS['post'];
      }
      // if no post return false
      if ( !$_post ) { return FALSE; }
      // check whether post matches term belongin to tax
      $return = is_object_in_term( $_post->ID, $tax, $term );
      // if error returned, then return false
      if ( is_wp_error( $return ) ) { return FALSE; }
      return $return;
  }
  
  /* Usage */
  
  /* Let’s say you’ve created a custom post type called “Films” which has a custom taxonomy of  “Genres” and you want to know if the current post is in the “western” genre, you would do as follows: */
  
  if (pa_in_taxonomy('genres', 'western')) {
  echo "Western films are great";
  } else {
  // do something else
  }
  /* Note I’m using the tax/term slug, but the is_object_in_term function in use will accept term id, slug, or array of either, so you could do: */
  if (pa_in_taxonomy('genres', array('western', 'drama', 'comedy'))) {}
  // or
  if (pa_in_taxonomy('genres', array(1, 2, 3)) {}
  
  
  // Source: http://alex.leonard.ie/2011/06/30/wordpress-check-if-post-is-in-custom-taxonomy/


?>