代码之家  ›  专栏  ›  技术社区  ›  user2812779

循环中仅显示父类别标题一次

  •  0
  • user2812779  · 技术社区  · 6 年前

    我有一个while循环,循环通过一个定制的post类型称为“经销商”。

    每个经销商都有一个父类别,其中一些经销商有子类别。

    我只想在循环中显示父类别的名称一次,但它不起作用。

    到目前为止我得到了这个代码,但是现在它显示了3次“Nederland”,因为有3个经销商的父类别是“Nederland”。

    <?php while ( have_posts() ): the_post();
    $categories = get_the_category(); 
    $cat_name = $categories[0]->cat_name;
    
    $titel_categorie = false;
    
    if($cat_name == "Nederland" && !$titel_categorie)
    {
        echo "Nederland";
        $titel_categorie = true;
    }
    ?>
    
    <div class="col-lg-4">
        <span class="dealer-title"><?php the_title(); ?></span>
        <span class="dealer-plaats"><?php the_field('plaats'); ?></span>
        <span class="dealer-plaats"><?php the_field('telefoonnummer'); ?></span>
        <span class="dealer-plaats"><?php the_field('website'); ?></span>
        <span class="dealer-plaats"><?php the_field('e-mailadres'); ?></span>
    </div>
    <?php endwhile; ?>
    

    我以前用过一次这种方法,它确实有效,但现在不行了。

    有人有别的建议吗?

    编辑

    谢谢大家的回答,他们中的一些人做了一些工作,但没有解决我现在的问题。

    我设法让它在while循环之外运行,但我也得到了子类别。

    <?php 
    $titel_categorie_nederland = false;
    $titel_categorie_belgie = false;
    $titel_categorie_italie = false;
    $titel_categorie_polen = false;
    $titel_categorie_noord_brabant = false;
    while ( have_posts() ): the_post();
    $categories = get_the_category(); 
    $cat_name = $categories[0]->cat_name;
    
    
    if($cat_name == "Nederland" && !$titel_categorie_nederland)
    {
    ?>
    <div class="col-lg-12"><h3>Nederland</h3></div>
    <?php
        $titel_categorie_nederland = true;
    }
    
    if($cat_name == "Polen" && !$titel_categorie_polen)
    {
    ?>
    <div class="col-lg-12"><h3>Polen</h3></div>
    <?php
        $titel_categorie_polen = true;
    }
    
    if($cat_name == "Belgie" && !$titel_categorie_belgie)
    {
    ?>
    <div class="col-lg-12"><h3>Belgie</h3></div>
    <?php
        $titel_categorie_belgie = true;
    }
    if($cat_name == "Italie" && !$titel_categorie_italie)
    {
    ?>
    <div class="col-lg-12"><h3>Italie</h3></div>
    <?php
        $titel_categorie_italie = true;
    }
    ?>
    
    <div class="col-lg-4">
        <span class="dealer-title"><?php the_title(); ?></span>
        <span class="dealer-plaats"><?php the_field('plaats'); ?></span>
        <span class="dealer-plaats"><?php the_field('telefoonnummer'); ?></span>
        <span class="dealer-plaats"><?php the_field('website'); ?></span>
        <span class="dealer-plaats"><?php the_field('e-mailadres'); ?></span>
    </div>
    <?php endwhile; ?>
    

    这是可行的,但不是一种干净的编码方式,展望未来,当一个新的国家加入时,这样做并不明智。

    我有一个自定义的邮政类型经销商,主要类别是国家和次类别是省。有没有办法用更干净的代码来显示它?

    https://imgur.com/a/4hcakmR

    3 回复  |  直到 6 年前
        1
  •  1
  •   party-ring    6 年前

    使用 break 会阻止你的循环

    foreach ( $terms as $term ) {
        $land_titel = $term->name;
    
        if($land_titel == "Nederland" && !$land_titel_nederland) {
            echo "Nederland";
            break;
        }
    }
    

    如果您想在另一个循环中中断循环(例如while循环),可以使用 continue .

    foreach ( $terms as $term ) {
        $land_titel = $term->name;
    
        if($land_titel == "Nederland" && !$land_titel_nederland) {
            echo "Nederland";
            continue;
        }
    }
    
        2
  •  0
  •   RustyBadRobot    6 年前

    这应该做到:

    <?php
    
    $post_type = 'dealers';
    
    // Get all the taxonomies for this post type
    $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
    
    foreach( $taxonomies as $taxonomy ) : 
    
        // Gets every "category" (term) in this taxonomy to get the respective posts
        $terms = get_terms( array( 
        'taxonomy' => $taxonomy,
        'parent'   => 0
        ) );
    
        foreach( $terms as $term ) :
    
            echo "<h1>".$term->name."</h1>";
    
            $posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1" );
    
            if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
            ?>
            <div class="col-lg-4">
              <span class="dealer-title"><?php the_title(); ?></span>
              <span class="dealer-plaats"><?php the_field('plaats'); ?></span>
              <span class="dealer-plaats"><?php the_field('telefoonnummer'); ?></span>
              <span class="dealer-plaats"><?php the_field('website'); ?></span>
              <span class="dealer-plaats"><?php the_field('e-mailadres'); ?></span>
            </div>
            <?php endwhile; endif;
    
        endforeach;
    
    endforeach;
    
    ?>
    
        3
  •  0
  •   UpLinK    6 年前

    设置在循环外(while)

    $titel_categorie = false;