admin管理员组

文章数量:1122832

I've got a custom post type / taxonomy. I've created a list that shows all the categories and the posts in them.

I have set up a top level category (taxonomy) that has two sub categories. I want my list to show only the second level categories and the posts in them. Currently it outputs both the sub categories and their posts correctly but it then also shows the top level category and all the posts within that too even if they are not directly in it.

  • Sub Category 1

    • Post 1
    • Post 2
  • Sub Category 2

    • Post 3
    • Post 4
  • Top Level Category

    • Post 1
    • Post 2
    • Post 3
    • Post 4

How do I exclude the top level category and it's list completly? E.g.

  • Sub Category 1

    • Post 1
    • Post 2
  • Sub Category 2

    • Post 3
    • Post 4

The code I used is below.

Ref: Loop through custom taxonomies and display posts

<?php
    $custom_terms = get_terms('service_categories');

    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'services',
            'tax_query' => array(
                array(
                    'taxonomy' => 'service_categories',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );

         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            echo '<h2>'.$custom_term->name.'</h2>';

            while($loop->have_posts()) : $loop->the_post();
             $title = get_the_title();
             $url = get_permalink();
             $serviceicon = get_field( 'select_icon' );
             ?>

                <div class="left-list2-box">
                    <a href="<?php echo $url; ?>">
                        <div class="left-list2-icon">
                            <div class="list-services-icons <?php echo $serviceicon ?>"></div>
                        </div>
                        <div class="left-list2-title">
                            <?php echo $title; ?>
                        </div>
                    </a>
                </div>

            <?php endwhile;
         }
    }
?>

I've got a custom post type / taxonomy. I've created a list that shows all the categories and the posts in them.

I have set up a top level category (taxonomy) that has two sub categories. I want my list to show only the second level categories and the posts in them. Currently it outputs both the sub categories and their posts correctly but it then also shows the top level category and all the posts within that too even if they are not directly in it.

  • Sub Category 1

    • Post 1
    • Post 2
  • Sub Category 2

    • Post 3
    • Post 4
  • Top Level Category

    • Post 1
    • Post 2
    • Post 3
    • Post 4

How do I exclude the top level category and it's list completly? E.g.

  • Sub Category 1

    • Post 1
    • Post 2
  • Sub Category 2

    • Post 3
    • Post 4

The code I used is below.

Ref: Loop through custom taxonomies and display posts

<?php
    $custom_terms = get_terms('service_categories');

    foreach($custom_terms as $custom_term) {
        wp_reset_query();
        $args = array('post_type' => 'services',
            'tax_query' => array(
                array(
                    'taxonomy' => 'service_categories',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
         );

         $loop = new WP_Query($args);
         if($loop->have_posts()) {
            echo '<h2>'.$custom_term->name.'</h2>';

            while($loop->have_posts()) : $loop->the_post();
             $title = get_the_title();
             $url = get_permalink();
             $serviceicon = get_field( 'select_icon' );
             ?>

                <div class="left-list2-box">
                    <a href="<?php echo $url; ?>">
                        <div class="left-list2-icon">
                            <div class="list-services-icons <?php echo $serviceicon ?>"></div>
                        </div>
                        <div class="left-list2-title">
                            <?php echo $title; ?>
                        </div>
                    </a>
                </div>

            <?php endwhile;
         }
    }
?>
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Dec 21, 2014 at 21:41 Legin76Legin76 1171 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For reference of other I've managed to get help with this with help from wordpress support forums.

You can specify a second parameter in get_terms to exclude specific term IDs:

$term_args = array( 'exclude' => array(5, 8) ); $custom_terms = get_terms('service_categories', $term_args);

In the case above, we're excluding terms with ID 5 and 8.

Reference: https://wordpress.org/support/topic/excluding-top-level-from-taxonomy-and-post-loop?replies=3

I added the code above before the foreach and it worked perfectly.

本文标签: Excluding top level from taxonomy and post loop