Related posts by taxonomy

时间:2015-04-29 作者:DarrenLee

我想显示与当前帖子相同的分类法(艺术家)的相关帖子。在相关帖子中,我不希望当前帖子出现。

我也在使用自定义的帖子类型。(雕塑)

这是我在下面使用的代码,但它显示了相关帖子中的当前帖子和所有帖子,而不仅仅是那些具有分类法的帖子(艺术家)

有人有解决方案吗

<div class="related-posts">
<h3 class="widget-title">Related Posts</h3>

<ul>
 <?php
 $query = new WP_Query(array(\'post_type\' => \'sculptures\', \'artist\' => get_the_term_list( $post->taxonomies, \'artist\' )));
 while ($query->have_posts()) : $query->the_post();
 ?>

 <div class="related-thumb"><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail(\'medium\'); ?></a></div>


<?php endwhile; wp_reset_query(); ?>

2 个回复
SO网友:m4n0

使用post__not_in 在您的查询中。

$query = new WP_Query(array(\'post_type\' => \'sculptures\', \'post__not_in\' => array($currentID), \'artist\' => get_the_term_list( $post->taxonomies, \'artist\' )));

SO网友:fxguillois

也许有人会这样想:

$artists = get_the_term_list( $post->taxonomies, \'artist\' );
unset($artists[$key]); // don\'t know the context, but get the term, and remove it from the array

$args=array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'artist\',
            \'field\' => \'id\',
            \'terms\' =>   $artists
        )
    ),
    \'post_type\' => \'sculptures\',
    \'post_status\' => \'publish\',
    \'posts_per_page\' => -1,
    \'caller_get_posts\'=> 1
);
$publishedQuery = new WP_Query($args);
while ($publishedQuery ->have_posts()) : $publishedQuery ->the_post();
...
endwhile; wp_reset_query();
只需调整$artists即可满足您的需求。

结束

相关推荐

Set a taxonomy as private

我创建了分类增值税,并将public设置为false。但在前端,我可以通过…/?增值税=22。如何保留该分类法供内部使用?下面是我为此分类法创建的代码。add_action( \'init\', \'gp_register_taxonomy_vat\' ); function gp_register_taxonomy_vat() { $labels = array( \'name\' => \'VAT\', \