按自定义分类在自定义帖子类型中显示相关帖子

时间:2012-02-23 作者:Jake Caputo

我试图通过自定义帖子类型(products)中的自定义分类法(product\\u标记)显示相关帖子。下面的代码可以工作,但它忽略了posts_per_page.

有什么想法吗?

                global $post;
                $terms = get_the_terms( $post->ID , \'product_tags\', \'string\');
                $do_not_duplicate[] = $post->ID;

                if(!empty($terms)){
                    echo \'<strong>Related Products</strong>\';
                    foreach ($terms as $term) {
                        query_posts( array(
                        \'post_type\' => \'products\',
                        \'product_tags\' => $term->slug,
                        \'posts_per_page\' => 3,
                        \'ignore_sticky_posts\' => 1,
                        \'orderby\' => \'rand\',
                        \'post__not_in\' => $do_not_duplicate) );
                        if(have_posts()) {
                            while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
                            <div class="single_related">
                            <?php if (has_post_thumbnail()) { ?>
                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
                                    <?php the_post_thumbnail( \'related_sm\', array(\'alt\' => get_the_title()) ); ?>
                                </a>
                            <?php } else { ?>
                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
                            <?php } ?>
                            </div>
                            <?php endwhile; wp_reset_query();
                        }
                    }
                }

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

如果要查询多个分类术语,可以使用更高级的,tax_query WP\\u查询的参数。比如说

//Get array of terms
$terms = get_the_terms( $post->ID , \'product_tags\', \'string\');
//Pluck out the IDs to get an array of IDS
$term_ids = wp_list_pluck($terms,\'term_id\');

//Query posts with tax_query. Choose in \'IN\' if want to query posts with any of the terms
//Chose \'AND\' if you want to query for posts with all terms
  $second_query = new WP_Query( array(
      \'post_type\' => \'products\',
      \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'product_tags\',
                        \'field\' => \'id\',
                        \'terms\' => $term_ids,
                        \'operator\'=> \'IN\' //Or \'AND\' or \'NOT IN\'
                     )),
      \'posts_per_page\' => 3,
      \'ignore_sticky_posts\' => 1,
      \'orderby\' => \'rand\',
      \'post__not_in\'=>array($post->ID)
   ) );

//Loop through posts and display...
    if($second_query->have_posts()) {
     while ($second_query->have_posts() ) : $second_query->the_post(); ?>
      <div class="single_related">
           <?php if (has_post_thumbnail()) { ?>
            <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail( \'related_sm\', array(\'alt\' => get_the_title()) ); ?> </a>
            <?php } else { ?>
                 <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            <?php } ?>
       </div>
   <?php endwhile; wp_reset_query();
   }
Note: 这似乎是一个“二级循环”,即不是“主”循环(显示帖子/单个帖子)。如果是这样,你不应该被使用query_posts. 看到这个了吗excellent question.

SO网友:Zach Lysobey

这可能不是正确的解决方案,但我会从行动开始$term->slug:

foreach ($terms as $term) {
  $termSlug = $term->slug;
  query_posts( array(
    \'post_type\' => \'products\',
    \'product_tags\' => $termSlug,
    \'posts_per_page\' => 3,
    ...

结束

相关推荐

Pagination on custom taxonomy

我很难做到这一点。这不是我第一次遇到分页问题,或者说是WPs URL系统的问题。基本上我有这个URL:http://example.com/location/dc 并加载taxonomy-location.php 样板现在,我将为主题添加分页功能。所以我有一个URL:http://example.com/location/dc/page/2 它不会加载taxonomy-location.php 模板,它实际上加载404 样板这是调试栏显示的内容:看起来它得到了正确的值,WP只是没有加