如何隐藏子类别并显示其父类别

时间:2021-12-14 作者:TELESISTEMA

我有以下示意图:

    the world
        - Cover the world.
    
    The country
        -Covered the country.
    
    theater
        - theater cover.

etc...
我希望当我选择任何子类别时,父类别都会出现。

我的代码:

add_filter(\'get_the_terms\', \'hide_categories_terms\', 10, 3);

function hide_categories_terms($terms, $post_id, $taxonomy){

    // define which category IDs you want to hide
    $excludeIDs = array(31, 32, 33, 34, 35, 36, 37, 38, 96);

    // get all the terms 
    $exclude = array();
    foreach ($excludeIDs as $id) {
        $exclude[] = get_term_by(\'id\', $id, \'category\');
    }

    // filter the categories
    if (!is_admin()) {
        foreach($terms as $key => $term){
            if($term->taxonomy == "category"){
                foreach ($exclude as $exKey => $exTerm) {
                    if($term->term_id == $exTerm->term_id) unset($terms[$key]);
                }
            }
        }
    }

    return $terms;
}
但只需隐藏子类别。

1 个回复
SO网友:Aboelabbas

据我所知,这就是你想要实现的目标:

如果将帖子分配给子类别,则将其删除,并在该帖子的类别列表中仅显示其父类别。

您可以使用以下代码为任何子类别实现这一点:

add_filter(\'get_the_terms\', function($terms, $postId, $taxonomy) {

    // Return early if not a category, there is an error, or we are on the dashboard.
    if(\'category\' !== $taxonomy || is_wp_error($terms) || is_admin()) {
        return $terms;
    }

    // Create an array that will hold only parent categories
    $parents = [];

    // Loop on all the terms, keep the parents or get the parent of any child
    foreach ($terms as $term) {

        // This is a parent, add it and continue
        if(! $term->parent) {
            $parents[$term->term_id] = $term;
            continue;
        }

        // This is a child get its parent and add it
        $parent = get_term($term->parent, $taxonomy);
        if(is_a($parent, \'WP_Term\')) {
            $parents[$parent->term_id] = $parent;
        }

    }

    // Finally, reset the array keys and return it
    return array_values($parents);

}, 10, 3);
但是,如果只想对选定的子类别列表执行此操作,请使用以下代码:

add_filter(\'get_the_terms\', function($terms, $postId, $taxonomy) {

    // Return early if not a category, there is an error, or we are on the dashboard.
    if(\'category\' !== $taxonomy || is_wp_error($terms) || is_admin()) {
        return $terms;
    }

    // Categories to exclude
    $excludedIds = [31, 32, 33, 34, 35, 36, 37, 38, 96];

    // Create an array that will hold only not excluded categories
    $categoriesToKeep = [];

    // Loop on all the terms, and replace the excluded categories with their parents
    foreach ($terms as $term) {

        // This is not in the excluded list, add it and continue
        if(! in_array($term->term_id, $excludedIds)) {
            $categoriesToKeep[$term->term_id] = $term;
            continue;
        }

        // This an excluded category, replace it with its parent
        $parent = get_term($term->parent, $taxonomy);
        if(is_a($parent, \'WP_Term\')) {
            $categoriesToKeep[$parent->term_id] = $parent;
        }

    }

    // Finally, reset the array keys and return it
    return array_values($categoriesToKeep);

}, 10, 3);

相关推荐

搜索格式.php覆盖不起作用

我正在上Udemy课程,学习如何制作Wordpress插件和主题。我正在观看的视频让我通过searchform用一个自定义的搜索小部件覆盖默认的搜索小部件。php。根据我收集的信息,只要我遵循以下步骤,该文件应自动加载主题的其余部分:/主题目录/功能。php/* Some other code */ include ( get_theme_file_path(\'/includes/widgets.php\') ); add_action("widgets_init",