title: Show Parent Category instead of First Category - Wordpress
body: <p>I have inherited a website and the code I currently have seems to display the first category, judged by alphabetical order I think, on the post loop of a custom post... </p>
  
  <p>I have this code that's pulling through category name and the title of the post: </p>
  
  <pre><code>class SeedPost {
  
      public $post_id;
      public $post_type;
  
      function __construct($post_id) {
          $this-&gt;post_id = $post_id;
          $this-&gt;post_type = get_post_type($this-&gt;post_id);
      }
  
      function display($twocol = false) {
          global $post;
  
          $post = get_post($this-&gt;post_id);
  
          $cols = $twocol ? 'two' : 'three';
  
          setup_postdata($post);
  
          if($this-&gt;post_type == 'portfolio') {
              $overlay_class = 'item__overlay--portfolio';
          } else {
              $overlay_class = 'item--cat-' . SeedHelpers::first_category();
          }
          ?&gt;
          &lt;a href="&lt;?php the_permalink(); ?&gt;"&gt;
          &lt;div class="item item--&lt;?php echo $cols; ?&gt;"&gt;
              &lt;?php
              if(has_post_thumbnail()) {
                  the_post_thumbnail('news-archive', array('class' =&gt; 'item--three__child'));
              }
              ?&gt;
  
                  &lt;div class="item__overlay &lt;?php echo $overlay_class; ?&gt;"&gt;
                      &lt;span class="item__cat-title item__cat-title--overlay"&gt;&lt;?php echo SeedHelpers::first_category($this-&gt;post_type); ?&gt;&lt;/span&gt;
                      &lt;?php get_cat_name( $cat_id ) ?&gt;
                      &lt;h4 class="item__title"&gt;&lt;?php the_title(); ?&gt;&lt;/h4&gt;
                      &lt;!--    &lt;?php the_excerpt(); ?&gt; --&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
  
          &lt;/div&gt;
          &lt;/a&gt;
          &lt;?php
          wp_reset_postdata();
      }
  </code></pre>
  
  <p>The bit of code you will notice is: </p>
  
  <pre><code>SeedHelpers::first_category($this-&gt;post_type)
  </code></pre>
  
  <p>This relates to a function, I believe, that will display the first of the category assigned to this post.</p>
  
  <p>This function is here: </p>
  
  <pre><code>static function first_category($post_type = 'post') {
          if($post_type == 'post') {
              $category = get_the_category();
  
              if($category) {
                  return $category[0]-&gt;cat_name;
              }
  
              return false;
          } elseif($post_type == 'portfolio') {
              $category = get_the_terms(get_the_ID(), 'portfolio-category');
  
              if($category) {
                  return $category[0]-&gt;name;
              }
  
              return false;
          }
      }
  </code></pre>
  
  <p>Each of my posts have one main category and multiple child categories, I would like to alter the code so it shows the parent sub category only...</p>
  
  <p>I have tried most things I have found online but I can't seem to make it display properly...</p>
  
  <p>EDIT >>>>>>>> I also have this bit of code underneath the bit above - not sure if this has anything to do with it?</p>
  
  <pre><code>static function category_shorthand() {
          $category = get_the_terms(get_the_ID(), 'portfolio-category');
  
          if($category) {
              $category_id = $category[0]-&gt;term_id;
              $shorthand = get_field('shorthand', 'portfolio-category_' . $category_id);
  
              if($shorthand) {
                  return $shorthand;
              }
  
              return $category[0]-&gt;name;
          }
  
          return false;
      }
  </code></pre>
  
  <p>The site is here: <a href="http://ideedev.co.uk/newseed/portfolio/" rel="nofollow noreferrer">http://ideedev.co.uk/newseed/portfolio/</a> and displays the category in the rollover boxed on a portfolio item...</p>