web-dev-qa-db-ja.com

カスタム投稿タイプの関係

動画のカスタム投稿タイプがあります。私は他の投稿カテゴリとそれを関連付ける必要があります。そして私は10のカテゴリーがあります

このような :

Post Category - Video Category
Tech            Tech
Marketing       Marketing
News            News
.....................

私はこれをしたいのです。私はポストカテゴリーテックを持っており、私はカスタムポストタイプカテゴリー(タクソノミー)テックを持っています.

現在Tech(Post)Categoryを表示しているときに、(サイドバーに)Techビデオを一覧表示する方法を教えてください。

1
Genxer

あなたの質問から、私はあなたの分類学用語名があなたのカテゴリー名と一致するように集めます。この場合、alwaysが当てはまります。カスタム投稿タイプをカテゴリ別スラッグでクエリしないのはなぜですか。正確な名前がわかりませんので、videoにしました。

// get category slug
$cat = get_category( get_query_var( 'cat' ) );
$cat_slug = $cat->slug;

// query your custom post type (video?), assuming 
// slugs match for your category name and taxonomy term name
$my_args = array(
    'post_type' => 'video',
    'tax_query' => array(
        array(
            'taxonomy' => 'your_taxonomy_name',
            'field' => 'slug',
            'terms' => $cat_slug
        )
    )
);

$my_query = new WP_Query($my_args);

while ($my_query->have_posts()) {
    $my_query->the_post();
    // do the usual thing
}
3
montrealist