Wordpress:根据自定义分类术语动态显示相关帖子?

时间:2016-05-19 19:49:36

标签: php wordpress custom-taxonomy

我需要将相关帖子添加到我的自定义帖子类型中(使用当前的分类术语)。我知道如何显示它们,我只需要能够检索当前选择的分类术语。

这就是我正在做的事情:

$args = array(
    'post_type'         => 'mycustomposttype',
    'posts_per_page'    => 3,
    'orderby'           => 'rand',
    'tax_query' => array(
        array(
            'taxonomy'  => 'thetaxonomy',
            'terms'     => 'theterm',
            'field'     => 'slug',
        )
    ),
);

我需要动态填充" thetaxonomy "和" theterm "基于页面上当前选择的内容。获得这个术语很容易,但我很难获得" thetaxonomy "蛞蝓。

提前致谢!

想出来了!

如果有人想要完成同样的事情

$currentTax = get_the_taxonomies($post->ID);
foreach($currentTax as $slug => $tax) {
    $currentSlug = $slug;
};
$theTerms = array();
$term_list = wp_get_post_terms($post->ID, $currentSlug, array("fields" => "all"));
foreach($term_list as $term_single) {
    array_push($theTerms, $term_single->slug);
}

所以查询看起来像这样......

$args = array(
    'post_type'         => 'mycustomposttype',
    'posts_per_page'    => 3,
    'orderby'           => 'rand',
    'tax_query' => array(
        array(
            'taxonomy'  => $slug,
            'terms'     => $theTerms,
            'field'     => 'slug',
        )
    ),
);

0 个答案:

没有答案