2017-05-16 16 views
0

私は現在ウェブサイトを継承しており、私が現在持っているコードは、カスタム投稿のポストループでアルファベット順に判断される最初のカテゴリを表示するようです...最初のカテゴリ - Wordpressの代わりに親カテゴリを表示

私はカテゴリ名とポストのタイトルを通じて引っ張っています。このコードを持って:あなたは気づくでしょうコードのビットが

class SeedPost { 

    public $post_id; 
    public $post_type; 

    function __construct($post_id) { 
     $this->post_id = $post_id; 
     $this->post_type = get_post_type($this->post_id); 
    } 

    function display($twocol = false) { 
     global $post; 

     $post = get_post($this->post_id); 

     $cols = $twocol ? 'two' : 'three'; 

     setup_postdata($post); 

     if($this->post_type == 'portfolio') { 
      $overlay_class = 'item__overlay--portfolio'; 
     } else { 
      $overlay_class = 'item--cat-' . SeedHelpers::first_category(); 
     } 
     ?> 
     <a href="<?php the_permalink(); ?>"> 
     <div class="item item--<?php echo $cols; ?>"> 
      <?php 
      if(has_post_thumbnail()) { 
       the_post_thumbnail('news-archive', array('class' => 'item--three__child')); 
      } 
      ?> 

       <div class="item__overlay <?php echo $overlay_class; ?>"> 
        <span class="item__cat-title item__cat-title--overlay"><?php echo SeedHelpers::first_category($this->post_type); ?></span> 
        <?php get_cat_name($cat_id) ?> 
        <h4 class="item__title"><?php the_title(); ?></h4> 
        <!-- <?php the_excerpt(); ?> --> 
        </div> 
       </div> 

     </div> 
     </a> 
     <?php 
     wp_reset_postdata(); 
    } 

SeedHelpers::first_category($this->post_type) 

これはfunctiに関連しますこの投稿に割り当てられたカテゴリの最初のものが表示されます。

この関数はここにある:

static function first_category($post_type = 'post') { 
     if($post_type == 'post') { 
      $category = get_the_category(); 

      if($category) { 
       return $category[0]->cat_name; 
      } 

      return false; 
     } elseif($post_type == 'portfolio') { 
      $category = get_the_terms(get_the_ID(), 'portfolio-category'); 

      if($category) { 
       return $category[0]->name; 
      } 

      return false; 
     } 
    } 

私のポストのそれぞれが一つのメインカテゴリーと複数の子カテゴリを持って、私はそれが唯一の親サブカテゴリーを示してコードを変更したいと思います...

私はオンラインで見つけたものを試しましたが、正しく表示されないようです...

EDIT >>>>>>>>このビットは上記のビットの下にありますこれに関係があるかどうか確かめてください。

static function category_shorthand() { 
     $category = get_the_terms(get_the_ID(), 'portfolio-category'); 

     if($category) { 
      $category_id = $category[0]->term_id; 
      $shorthand = get_field('shorthand', 'portfolio-category_' . $category_id); 

      if($shorthand) { 
       return $shorthand; 
      } 

      return $category[0]->name; 
     } 

     return false; 
    } 

サイトはここにある:私の知る限りでは、ドキュメントを読んで、get_the_categoryがWP_Termオブジェクトの配列を返す必要がありますhttp://ideedev.co.uk/newseed/portfolio/とポートフォリオ品目に箱入りのロールオーバーでカテゴリを表示...

+0

お試しください$ category [0] - > category_parent' – RST

+0

ありがとうございます@RSTどこに追加しますか? –

+0

'$ category [0] - > category_parent'の代わりに' $ category [0] - > name'が来ます – RST

答えて

0

、どのparent(親のIDを含む)という名前の公開変数が含まれています。

親IDをパラメータとしてメソッドget_the_category_by_ID()を呼び出すと、その変数を使用して親カテゴリ名を取得できるはずです。だから、買ってあげる:

if($category) { 
    $parentId = $category[0]->parent; // contains the parent category ID 
    return get_the_category_by_ID($parentId); // Returns the name of the category 
} 

代わりの

if($category) { 
    return $category[0]->cat_name; 
} 

ドキュメント:

+0

この@Erikをありがとう - 何もしていないようです。 ? Iveは一度に両方を試しましたが、まだ動作しません - おそらく何か不足していますか? –

+0

私はちょうど私が見つけたコードの別のビットでポストを更新しました - それはカテゴリとは関係ありません... –

+0

まあ、あなたは値を 'エコー'であなたのコードをデバッグしようとすることができます。あるいは 'print_r($ category)'なので、その変数の内容を見ることができます。 'echo '

'; print_r($category); echo '
''のようなものは、もっと明確な結果をもたらすはずです。そこから、 'parent'変数に値などが含まれているかどうかを確認する必要があります。 –

関連する問題