web-dev-qa-db-ja.com

分類用語はどの投稿に属していますか?

URLから取得した特定の種類のカスタム分類用語をすべて表示するページがあります。このコードを使ってすべての用語を取得します。

$args = array(
'post_type' => 'mycustomposttype',
'programtype' => ’my-custom-taxonomy-term
);
$programtype = new WP_Query($args);
while ( $programtype->have_posts() ) : $programtype->the_post();
$terms = get_the_terms( $post->ID, 'my-custom-taxonomy );

これはすべてうまくいきますが、それから自分が属する元のカスタム投稿にリダイレクトするために各用語を押すときにリンクを作成したいと思います。

例を挙げてみましょう。ID19のカスタム投稿タイプ「Programblock」には、「イベント」と呼ばれる3つのカスタム分類用語が付いています。上記のページに3つの用語がリストされていますが、それからこれらの用語をハッシュタグとして投稿IDを持つカスタム投稿タイプページ“ Programblock”に戻すリンクを作成します。 http://www.my-domain.com/programblock#19

どのようにしてこれを元に戻すことができますか?「この記事はどの用語に属していますか」と尋ねる代わりに、「この用語はどの記事に属していますか」と尋ねたい.

私は自分自身を十分明確に説明したことを望みます、そうでなければ尋ねてください。

誠実
- メスティカ

1
Mestika

あなたはterm_linkフィルタを使ってそれを行うことができます。おおよそ次のようなものがあります。

function my_term_link($termlink, $term, $taxonomy) {
   global $post;
   if ($taxonomy == 'my-custom-taxonomy') {
      return get_permalink( $post->ID ) . '#' . $term->term_id;
   }
}

while ( $programtype->have_posts() ) : $programtype->the_post();
   $terms = get_the_terms( $post->ID, 'my-custom-taxonomy' );
   add_filter('term_link', 'my_term_link', 10, 3);
   foreach ($terms as $term) {
      $link = get_term_link( $term, 'my-custom-taxonomy' );
      // Use link here
   }
   remove_filter('term_link', 'my_term_link', 10, 3);
endwhile;
1
Ben Huson