Categoria padre di un post in Wordpress


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /**
  2.  * Ritorna la categoria padre di un post
  3.  *
  4.  * @return
  5.  * @param object $post_id
  6.  */
  7. function get_parent_category( $post_id ) {
  8. $cats = wp_get_object_terms($post_id, 'category', array('fields' => 'all') );
  9. foreach( $cats as $key => $cat ) if( $cat->parent == 0) return $cat;
  10. return null;
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.