1

カスタム分類に基づいて関連する投稿を表示しようとしています。wordpress.org で、そのような動作をするクエリを見つけました。ただし、元の投稿は結果で複数回複製されます。(単語は私が使用するカスタム分類法の名前です) showpost が設定されている量に応じて、単一の投稿が複製されるようです。これを引き起こす可能性のあるアイデアはありますか?

コード:

<?php
//for in the loop, display all "content", regardless of post_type,
//that have the same custom taxonomy (e.g. words) terms as the current post
$backup = $post;  // backup the current object
$found_none = '<h2>No related posts found!</h2>';
$taxonomy = 'words';//  e.g. post_tag, category, custom taxonomy
$param_type = 'words'; //  e.g. tag__in, category__in, but genre__in will NOT work
$post_types = get_post_types( array('public' => true), 'names' );
$tax_args=array('orderby' => 'none');
$tags = wp_get_post_terms( $post->ID , $taxonomy, $tax_args);
if ($tags) {
  foreach ($tags as $tag) {
    $args=array(
      "$param_type" => $tag->slug,
      'post__not_in' => array($post->ID),
      'post_type' => $post_types,
      'showposts'=>5,
      'caller_get_posts'=>1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
        <?php $found_none = '';
      endwhile;
    }
  }
}
if ($found_none) {
echo $found_none;
}
$post = $backup;  // copy it back
wp_reset_query(); // to use the original query again
?>
4

2 に答える 2

0

私としては、このプラグインをカスタム分類関連の投稿に使用しています。このプラグインがあなたの問題に役立つことを願っています。

于 2013-01-21T09:41:28.653 に答える
0

foreach重複しているのはループ内です。そのコードは効果的に言っています。

  1. 分類タイプのすべての用語を取得する$param_type
  2. 用語ごとに、その用語でタグ付けされた 5 つの投稿を取得します

そのため、同じ分類法の複数の用語でタグ付けされた投稿がある場合、複数回表示される可能性があります。

クエリされた投稿を配列に繰り返し追加して、post__not_inそれらが再び表示されないようにすることができます。

  1. $post_not_in = array($post->ID);すぐ上に追加if ($tags) {

  2. post__not_in' => array($post->ID),次に、行を に置き換えpost__not_in' => $post_not_in,ます。

  3. 最後に、ループ$post_not_in[] = get_the_ID();内にドロップします。while$found_none = '';

于 2010-06-08T01:47:34.477 に答える