显示具有相同分类术语的帖子

时间:2014-03-29 作者:user1374796

我试图在循环中显示5个相关帖子,这些相关帖子是共享相同分类值的帖子。e、 g.我有一个自定义分类设置,名为venues 因此,每个帖子都有一个venue 分配给它的分类法值,因此在每个帖子中,我想显示其他5篇共享相同分类法值的帖子(即,在同一地点)
到目前为止,我使用的代码不太正确:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$custom_terms = get_terms(\'venues\');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array(\'post_type\' => \'listings\',
    \'posts_per_page\' => 5,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'venues\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {

        while($loop->have_posts()) : $loop->the_post(); ?>
        <div class="listing-title"><?php the_title(); ?></div>     
        <?php endwhile;
     }
}
?>
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>
它成功地显示了5篇文章,但它们只是来自相同文章类型的5篇文章,而不是循环中共享相同分类值的5篇文章。如有任何建议,将不胜感激!

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

完全未经测试,我不能百分之百肯定我理解你的问题,但这(理论上)应该得到5个帖子,它们与当前帖子共享任何一个相同的场所。我可能会建议在此基础上添加一些瞬态,这样您就不会经常运行查询。

如果它不起作用,我怀疑我的税务查询的语法有点不正确。它总是吸引我,因为它是一个数组的数组。

//get the post\'s venues
$custom_terms = get_terms(\'venues\');

if( $custom_terms ){

    // going to hold our tax_query params
    $tax_query = array();

    // add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
    if( count( $custom_terms > 1 ) )
        $tax_query[\'relation\'] = \'OR\' ;

    // loop through venues and build a tax query
    foreach( $custom_terms as $custom_term ) {

        $tax_query[] = array(
            \'taxonomy\' => \'venues\',
            \'field\' => \'slug\',
            \'terms\' => $custom_term->slug,
        );

    }

    // put all the WP_Query args together
    $args = array( \'post_type\' => \'listings\',
                    \'posts_per_page\' => 5,
                    \'tax_query\' => $tax_query );

    // finally run the query
    $loop = new WP_Query($args);

    if( $loop->have_posts() ) {

        while( $loop->have_posts() ) : $loop->the_post(); ?>

        <div class="listing-title"><?php the_title(); ?></div>     
        <?php endwhile;
    }

    wp_reset_query();

}?>

SO网友:Juha

感谢您提供了好的解决方案,但这一解决方案将为您提供具有相同分类法中任何术语的帖子。但是,如果您只想要与当前帖子具有相同术语的帖子,则需要像这样修改代码。这个已经过测试,应该可以用了。

 <?php
        //get the post\'s venues
    $custom_terms = wp_get_post_terms($post->ID, \'venues\');

    if( $custom_terms ){

        // going to hold our tax_query params
        $tax_query = array();

        // add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
        if( count( $custom_terms > 1 ) )
            $tax_query[\'relation\'] = \'OR\' ;

        // loop through venus and build a tax query
        foreach( $custom_terms as $custom_term ) {

            $tax_query[] = array(
                \'taxonomy\' => \'venues\',
                \'field\' => \'slug\',
                \'terms\' => $custom_term->slug,
            );

        }

        // put all the WP_Query args together
        $args = array( \'post_type\' => \'listings\',
                        \'posts_per_page\' => 5,
                        \'tax_query\' => $tax_query );

        // finally run the query
        $loop = new WP_Query($args);

        if( $loop->have_posts() ) {

            while( $loop->have_posts() ) : $loop->the_post(); ?>

            <div class="listing-title"><?php the_title(); ?></div>     
            <?php 

            endwhile;

        }

        wp_reset_query();

    }?>

结束

相关推荐

How to change taxonomy slug?

我有一些注册的分类法,如下所示:/********************************* Género *****************************/ function genre_init() { $labels_genre = array( \'name\' => _x( \'Géneros\', \'taxonomy general name\' ), \'singular_name\' => _