【问题标题】:Display subcategories of custom categories - WordPress显示自定义类别的子类别 - WordPress
【发布时间】:2016-10-08 22:13:19
【问题描述】:

这是我所拥有的:

    $args = array(
        'type' => 'post',
        'child_of' => 0,
        'parent' => '',
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => 1,
        'hierarchical' => 1,
        'exclude' => '',
        'include' => '',
        'number' => '',
        'taxonomy' => 'directory-category',
        'pad_counts' => false
    );

这让我知道了类别。

我想要的是获得这个directory-category 分类的子类别。

关于如何做到这一点的任何想法?

我不是在寻求解决方案,只是一个建议或有人给我指路。

谷歌搜索没有帮助:/

这是截图HERE

【问题讨论】:

    标签: wordpress field parent-child categories


    【解决方案1】:

    您说您不想直接回答,但实际上您想使用此处找到的 get_terms:

    https://developer.wordpress.org/reference/functions/get_terms/

    SELECT * from prod_term_taxonomy WHERE parent = 0;

    更新:

    // Using your specific parent taxonomy id of 214 the query is below
    global $wpdb;
    $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");
    
    // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...
    
    $child_cat_array = array();
    
    foreach ($results as $result) {
        $term = get_term( $result->term_id, $taxonomy );
        $name = $term->name;
        // the slug will be used for querying for posts
        $slug = $term->slug;
        // this will push the slug of the child category into the array for querying posts later
        array_push($child_cat_array, $slug);
    }
    

    然后您可以像这样修改您的 get_posts 查询:

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'directory-category',
                'field' => 'slug',
                'terms' => $child_cat_array
            )
        )
    );
    
    $postslist = get_posts( $args );
    

    【讨论】:

    • 您是否正在寻找与这些子类别相关的帖子?
    • 感谢您的建议。我已经检查了get_terms(),但我正在寻找一些更具体的东西,哈哈
    • 您可能需要使用全局 $wpdb 查询 WordPress 数据库。如果你愿意,我可以给你答案:) 你熟悉 SQL 吗?因为您将查询它们在 WordPress 数据库中的确切位置。不幸的是,没有内置的 WordPress 功能来检索它们。
    • 但是,您是要动态获取所有子标签或与之关联的帖子,还是两者兼而有之?
    • 嗯,是的,我正在尝试两者兼得。如果你能做到这一点,那将是非常可爱的!如果可以的话,请给我答案。谢谢
    【解决方案2】:

    如果您想根据您提供的单个类别显示子类别和相关帖子的列表,您可以使用以下代码。确保使用您自己的分类名称、post_type 和术语:

    function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
        $category = get_term_by( 'slug', $term, $taxonomy );
        $cat_id   = $category->term_id;
    
        // Get all subcategories related to the provided $category ($term)
        $subcategories = get_terms(
            array(
                'taxonomy'   => $taxonomy,
                'parent'     => $cat_id,
                'orderby'    => 'term_id',
                'hide_empty' => true
            )
        );
        ?>
        <div>
            <?php
            // Iterate through all subcategories to display each individual subcategory
            foreach ( $subcategories as $subcategory ) {
    
                $subcat_name = $subcategory->name;
                $subcat_id   = $subcategory->term_id;
                $subcat_slug = $subcategory->slug;
    
                // Display the name of each individual subcategory with ID and Slug
                echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug  . '</h4>';
    
                // Get all posts that belong to this specific subcategory
                $posts = new WP_Query(
                    array(
                        'post_type'      => $post_type,
                        'posts_per_page' => -1, // <-- Show all posts
                        'hide_empty'     => true,
                        'order'          => 'ASC',
                        'tax_query'      => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'terms'    => $subcat_id,
                                'field'    => 'id'
                            )
                        )
                    )
                );
    
                // If there are posts available within this subcategory
                if ( $posts->have_posts() ):
                    ?>
                    <ul>
                        <?php
                        while ( $posts->have_posts() ): $posts->the_post();
                            //Show the title of each post with the Post ID
                            ?>
                            <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
                            <?php
                        endwhile;
                        ?>
                    </ul>
                    <?php
                else:
                    echo 'No posts found';
                endif;
    
                wp_reset_query();
            }
            ?>
        </div>
        <?php
    }
    ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );
    

    'name-of-your-taxonomy' 是主要分类的名称。例如:'victual_category'

    'name-of-your-post-type' 是您的帖子类型的名称。例如:'victual'

    'name-of-your-specific-term' 是您要使用的特定类别的名称,以便可以显示属于该类别的子类别。例如:“食物”

    所以如果我调用函数:

    ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );
    

    这将显示所有子类别及其各自属于食品类别分类法的帖子:

    子类别:开胃菜 - ID:35 - 蛞蝓:开胃菜

    • 帖子:薯条和莎莎酱 - ID:464
    • 帖子:Queso - ID:465

    子类别:炸玉米饼 - ID:36 - 蛞蝓:炸玉米饼

    • 职位:Al Pastor - ID:466
    • 职位:Fish Al Pastor - ID:467

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2012-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多