2016-05-02 3 views
0

を働いていない....Wordpressのカスタムポストタイプ、カテゴリアーカイブは私がこれをグーグルで数時間を過ごしたが、何の喜びを持っていたしました

URL:http://workflow.wearepixel.co.uk/product/tall-cabinets/

私は、カスタムポストタイプ「製品」を持っており、カテゴリー「背の高いキャビネット」のcpt記事私は共有テンプレート(taxonomy.phpなど)に表示するカスタム投稿カテゴリが必要です

私はすべての標準テンプレート、category.php、taxonomy.php、archive.php、category-product.phpを試しました、taxonomy-product.php、archive-product.php ...そして神はスクリプトの数を知っています...

残念なことにtaxonomy-product-tall-cabinets.phpは動作しません。すべてのcptカテゴリのデフォルトテンプレートが必要です。

誰でも助けてください。

固定。 functions.phpにこれを追加します。

add_filter('pre_get_posts', 'query_post_type'); 
function query_post_type($query) { 
    if(is_category() || is_tag()) { 
    $post_type = get_query_var('post_type'); 
    if($post_type) 
     $post_type = $post_type; 
    else 
     $post_type = array('post','<strong>cpt</strong><strong>'); // replace cpt to your custom post type 
    $query->set('post_type',$post_type); 
    return $query; 
    } 
} 
+0

すべての製品を調べることができますか?

後でforeach文を使用すると、特定の商品がカテゴリに属する​​かどうかを知ることができます。 – christian

+0

カスタムポストタイプのアーカイブはできますが、そのカテゴリはありません:http://workflow.wearepixel.co.uk/product – leannekera

答えて

0

私は同様の問題を持っていた、私は私が使用したコードとあなたがここに置かれているコードに異なる見ることができる唯一のことは、あなたにもタグを探しています。

if(is_category() || is_tag()) { 

タグ部分は使用しませんが、タグは引き続き表示されます。また

このライン

 $post_type = array('post','<strong>cpt</strong><strong>'); // replace cpt to your custom post type 

試して、あなたの配列に 'nav_menu_item' を追加メニュー項目を追加する必要があり

$post_type = array('nav_menu_item', 'post','<strong>cpt</strong><strong>'); 

私の完全なコード:

add_filter('pre_get_posts', 'query_post_type'); 
 
function query_post_type($query) { 
 
    if(is_category()) { 
 
    $post_type = get_query_var('post_type'); 
 
    if($post_type) 
 
     $post_type = $post_type; 
 
    else 
 
     $post_type = array('nav_menu_item', 'post', 'case_study'); 
 
    $query->set('post_type',$post_type); 
 
    return $query; 
 
    } 
 
}

関連する問題