2017-02-09 10 views
0

このコードの操作方法は、「カテゴリ別の関連記事」のみを表示しますか? このコードは "single.php"内にあります 私はstackoverflowを実行しませんでしたが、解決策を見つけることができませんでした。誰でも助けてくれますか?WordPress(taxonomy)の関連記事カテゴリ

<?php 
    $postsPerPageq = 5; 

       $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
      'tax_query' => array(
     array(
      'taxonomy' => 'audiotop', 
      'field' => 'slug', 
      'terms' => 'top-audio' 
     ) 
    ) 
      ); 
       $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); 


    ?> 

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 


<?php endwhile; wp_reset_postdata();?> 
+1

これをチェックしてください: - http://wordpress.stackexchange.com/a/41273またはhttp://wordpress.stackexchange.com/questions/30039/how-to-display-related-posts-from-same-カテゴリ –

答えて

0

これはあなたに役に立てば幸い

<?php 
    $postsPerPageq = 5; 

      $allargw = array(
      'post_type' => 'audioplayer', 
      'posts_per_page' => $postsPerPageq, 
      'category_name'=>'Your category name', 
      'orderby' => 'title', 
      'order' => 'DESC', 
     'tax_query' => array(
    array(
     'taxonomy' => 'audiotop', 
     'field' => 'slug', 
     'terms' => 'top-audio' 
    ) 
) 
     ); 
      $topaudio = new WP_Query($allargw); 
while ($topaudio->have_posts()) : $topaudio->the_post(); 


?> 

<h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 

...あなたのallargw配列値でカテゴリ名を追加します!

更新答:あなたはこのように使用、自動的にカテゴリを追加したい場合は

..

wp_set_object_terms('your_post_id', 'your_category_name', 'your_taxonomy_name'); 

あなたはこれを試すことができます。..

+0

こんにちは、どのようにカテゴリを自動的に追加するのですか? 、例:ユーザーがカテゴリ1の投稿にアクセスします。その後、「関連する投稿」にはカテゴリ1の投稿のみが表示されます。 –

+0

実際に投稿を挿入してカテゴリを自動的に挿入したいのですか? – Preethi

0

を使用すると、簡単な記事のカテゴリー別の投稿を取得する場合このコードを確認してください:

<?php 
    $category = get_the_category($post->ID); 
    $cat_id = $category[0]->term_id; 
    $postsPerPageq = 5; 
    $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
       'cat' => $cat_id 

      ); 
    $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); ?> 
     <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 
    <?php endwhile; wp_reset_postdata();?> 

以下のコードをチェックしてください:

<?php 
    $term_list = wp_get_post_terms($post->ID, 'audiotop', array("fields" => "all")); 
    $term_id = $term_list[0]->term_id ; 
    $postsPerPageq = 5; 
    $allargw = array(
       'post_type' => 'audioplayer', 
       'posts_per_page' => $postsPerPageq, 
       'orderby' => 'title', 
       'order' => 'DESC', 
       'tax_query' => array(
      array(
       'taxonomy' => 'audiotop', 
       'field' => 'id', 
       'terms' => $term_id 
      ) 
     ) 
    ); 
    $topaudio = new WP_Query($allargw); 
    while ($topaudio->have_posts()) : $topaudio->the_post(); ?> 
     <h3><a href="<?php the_permalink(); ?>"><?php the_title();?></a></h3> 
    <?php endwhile; wp_reset_postdata();?> 

ご希望の分類方法で投稿してください。

+0

こんにちは、こんにちは、カテゴリを自動的に追加する方法、例:ユーザーがカテゴリにアクセスする1件の記事を投稿すると、 "関連する投稿"にはカテゴリ1の投稿のみが自動的に表示されます。カテゴリ2、カテゴリ3などでも同じことが起こります –

+0

1件を明確にしてください。 post1-> category1、category2、post2-> category3、category1のように、post1-> category1、post2-> category2、post3-> category1、post4-> category-> 2ではなく、 –

+0

各投稿が1つのカテゴリに割り当てられている場合、更新されたコードを見つけます。 –

1

まず、あなたは、あなたが用語があなたのWP_Queryでtax_query用スラグつかむ

get_the_terms (get_the_id(), 'your_custom_taxonomy'); 

を、この機能を使用して、現在のポスト用語をつかむ必要があります。

$postsPerPageq = 5; 
$terms_slugs = array(); 

//get the current post terms slug 
$terms = get_the_terms(get_the_id(), 'audiotop'); 
foreach ($terms as $term) { 
    $terms_slugs[] = $term->slug; 
} 

//WP_Query args 
$allargw = array(
    'post_type' => 'audioplayer', 
    'posts_per_page' => $postsPerPageq, 
    'orderby' => 'title', 
    'order'  => 'DESC', 
    'tax_query' => array(
    array(
     'taxonomy' => 'audiotop', 
     'field' => 'slug', 
     'terms' => $terms_slugs, 
    ) 
    ) 
); 

私はあなたの問題を解決することを願っています。

関連する問題