【问题标题】:WordPress parent and child taxonomy by post id按帖子 ID 分类的 WordPress 父子分类
【发布时间】:2015-01-26 12:38:33
【问题描述】:

我想像这样(住宅/公寓)在自定义帖子类型单页中显示分类术语(住宅/公寓)
作为父级的住宅
作为孩子的公寓
如何通过帖子 ID 和分类法使其显示为“属性类型”
我一直只使用不同的方式获取公寓,可能是因为未在帖子编辑器中选择父级,即使未选择它也如何显示它
谢谢。

【问题讨论】:

  • 你用什么代码来显示这个词?
  • get_the_term_list(),只返回 Apartment 而不是 Parent (Residential)

标签: wordpress taxonomy


【解决方案1】:

您可以添加过滤器以手动注入列表中术语的父项:

add_filter('get_the_terms', function ($terms, $post_id, $taxonomy)
{
    // Only modify the needed list
    if (get_post_type($post_id) == 'your_post_type' && $taxonomy == 'your_taxnomy' && is_array($terms))
    {
        $terms_with_parents = array();
        foreach ($terms as $term)
        {
            // For each term, check if there is a parent and adds it in the list
            $parent_term = $term->parent != 0 ? get_term_by('id', $term->parent, $taxonomy) : false;
            if (is_object($parent_term) && !is_wp_error($parent_term))
            {
                $terms_with_parents[] = $parent_term;
            }
            $terms_with_parents[] = $term;
        }
        return $terms_with_parents;
    }
    return $terms;
}, 10, 3);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多