显示与另一个分类术语相同的所有分类术语的帖子

时间:2016-02-14 作者:Arg Geo

我正在建立一个显示网络广播电台的网站。我创建了一个名为“stations”的自定义帖子类型和两个名为“流派”和“位置”的分类法。在我的页面中,我显示按流派分组的所有流派,无论其位置如何。代码:

<?php function get_stations_query($current_genre) {
    $query = new WP_Query( array(
                            \'post_type\'      => \'stations\',
                            \'post_status\'    => \'publish\',
                            \'posts_per_page\' => -1,
                            \'orderby\'        => \'title\',
                            \'order\'          => \'ASC\',
                            \'tax_query\'      => array(
                                    array(
                                        \'taxonomy\' => \'genres\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $current_genre
                                    )
                                )
                        ) );
    if ($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post();
        get_template_part(\'loop-stations\');
    endwhile; wp_reset_postdata(); else : ?>
        <p>Sorry, no radio stations found.</p>
    <?php endif; } ?>
<?php function generate_genres() {
    $genresrays = array (
        \'pop_genre\'       => \'Pop\',
        \'rock_genre\'      => \'Rock\',
        \'jazz_genre\'      => \'Jazz\',
        \'soul_genre\'      => \'Soul\',
        \'ethnic_genre\'    => \'Ethnic\',
        \'trance_genre\'    => \'Trance\',
        \'country_genre\'   => \'Country\',
        \'chill_out_genre\' => \'Chill Out\',
    );
    foreach ($genresrays as $genresray=>$genre_value) {
        $ex_term = term_exists($genre_value, \'genres\');
        if ($ex_term !== 0 && $ex_term !== null) { ?>
            <div class="clearfix"></div>
            <hr style="margin: 30px 0;">
            <div id="<?php echo strtolower(str_replace(\' \',\'-\',$genre_value)); ?>" class="clearfix">
                <h3 style="margin-top: 20px !important;"><?php echo $genre_value; ?></h3><br>
                <?php get_stations_query(strtolower($genre_value)); ?>
            </div>
        <?php }
    }
} ?>
我使用<?php generate_genres(); ?> 显示输出。我正在努力做的事情没有成功,就是通过同时按“位置”分类法查询这些帖子来显示完全相同的输出(在我的taxonomy-locations.php), 例如,帖子将显示如下:

-- POP --------------------
    Station X (USA)
    Station Y (USA)

-- ROCK -------------------
    Station C (USA)
    Station F (USA)

-- JAZZ -------------------
    Station L (USA)
    Station K (USA)

-- SOUL -------------------
    Station Z (USA)
    Station R (USA)
在自定义分类法模板中,我使用以下方法将当前分类法名称显示为标题:

<?php $loc_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) ); echo $loc_term->name; ?>
我试图通过同时使用2个分类法查询帖子来达到预期的结果\'relation\' => \'AND\' 但它不起作用。我尝试了很多变化,但在最好的情况下,我显示了所有电台,但分类“位置”被完全忽略了。我该怎么做?

EDIT

<?php
    $loc_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
    $query = new WP_Query( array(
                            \'post_type\'      => \'stations\',
                            \'post_status\'    => \'publish\',
                            \'posts_per_page\' => -1,
                            \'orderby\'        => \'title\',
                            \'order\'          => \'ASC\',
                            \'tax_query\'      => array(
                                    array(
                                        \'taxonomy\' => \'genres\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $current_genre,
                                        \'operator\' => \'AND\'
                                    ),
                                    array(
                                        \'taxonomy\' => \'locations\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $loc_term
                                    )
                                )
                        ) );
?>
在更改帖子查询(上面的代码)后,我设法只显示正确的帖子genres (自定义分类法)没有附加帖子,仍在显示。有人知道为什么/如何解决这个问题吗?

这是我得到的一个例子

-- POP --------------------
        Station X (USA)
        Station Y (USA)

-- ROCK -------------------  <- BLANK (shouldn\'t be displayed)
-- JAZZ -------------------  <- BLANK (shouldn\'t be displayed)

-- SOUL -------------------
    Station Z (USA)
    Station R (USA)

1 个回复
最合适的回答,由SO网友:Arg Geo 整理而成

我终于让它正常工作了。我正在发布解决方案,以便其他需要类似解决方案的人可以使用它。看来我需要做的是get_posts 具有与相同的参数WP_Query 并通过修改“foreach”中已经存在的“if”语句来检查是否有帖子。

All code

<?php function get_stations_query($current_genre) {
    $loc_term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
    $query = new WP_Query( array(
                            \'post_type\'      => \'stations\',
                            \'post_status\'    => \'publish\',
                            \'posts_per_page\' => -1,
                            \'orderby\'        => \'title\',
                            \'order\'          => \'ASC\',
                            \'tax_query\'      => array(
                                    array(
                                        \'taxonomy\' => \'genres\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $current_genre,
                                        \'operator\' => \'AND\'
                                    ),
                                    array(
                                        \'taxonomy\' => \'locations\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $loc_term->name
                                    )
                                )
                        ) );
    if ($query->have_posts()) : while ( $query->have_posts() ) : $query->the_post();
        get_template_part(\'loop-stations\');
    endwhile; wp_reset_postdata(); else : ?>
        <p>Sorry, no radio stations found.</p>
    <?php endif;
} ?>

<?php function generate_genres() {
    $genresrays = array (
        \'pop_genre\'       => \'Pop\',
        \'rock_genre\'      => \'Rock\',
        \'jazz_genre\'      => \'Jazz\',
        \'soul_genre\'      => \'Soul\',
        \'ethnic_genre\'    => \'Ethnic\',
        \'trance_genre\'    => \'Trance\',
        \'country_genre\'   => \'Country\',
        \'chill_out_genre\' => \'Chill Out\',
    );

    foreach ($genresrays as $genresray=>$genre_value) {
        $ex_term = term_exists($genre_value, \'genres\');
        $loc_term_s = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );

        $grab = get_posts(array(
                            \'post_type\'      => \'stations\',
                            \'post_status\'    => \'publish\',
                            \'tax_query\'      => array(
                                    array(
                                        \'taxonomy\' => \'genres\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $genre_value,
                                        \'operator\' => \'AND\'
                                    ),
                                    array(
                                        \'taxonomy\' => \'locations\',
                                        \'field\'    => \'name\',
                                        \'terms\'    => $loc_term_s->name
                                    )
                            ))
                );

        if (!empty($grab) && $ex_term !== 0 && $ex_term !== null) { ?>
            <div class="clearfix"></div>
            <hr style="margin: 30px 0;">
            <div id="<?php echo strtolower(str_replace(\' \',\'-\',$genre_value)); ?>" class="clearfix">
                <h3 style="margin-top: 20px !important;"><?php echo $genre_value; ?></h3><br>
                <?php get_stations_query(strtolower($genre_value)); ?>
            </div>
        <?php }

    }
} ?>

相关推荐