在While循环中显示一次父类别和子类别

时间:2018-10-17 作者:user2812779

我已经尝试了几件事,但不幸的是并没有以一种好的方式做到这一点。

我使用while循环来挑选来自不同国家和省份的经销商(自定义帖子类型)。在这个经销商循环中,我想将他们分为国家和省份。

经销商的主要类别是国家/地区,然后一些国家/地区可以选择一个子类别,即省份。

我想通过这种方式对它们进行分类:Categorized dealers

正如我所说,我已经尝试了几种方法,但效果并不完全符合我的要求。此外,在我看来,这也是太多的代码。

代码:

<?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; ?>
此代码仅用于主类别(国家),但不用于子类别(如果有)。也就是说,它也不起作用。

我知道一定有更简单的方法来实现这一点。有人能把我引向正确的方向吗?

2 个回复
SO网友:ville6000

这是未经测试的,但您已经执行了一个嵌套循环,其中循环父类别和子类别。当你找到一个正确的家长时,你会循环帖子并检查哪些帖子有正确的类别。

<?php
// Fetch categories in to an array
// Fetch provinces in to an array

// First loop your categories
foreach ( $categories as $category ) {
    printf( \'<h2>%s</h2>\', $category->name );

    // If we have provinces
    if ( ! empty( $provinces ) ) {
        // Let\'s loop all the provinces
        foreach ( $provinces as $province ) {
            // Does the province have an correct category
            if ( $province->category_parent === $category->term_id ) {
                printf( \'<h3>%s</h3>\', $province->name );

                // Loop your posts
                foreach  ($posts as $post ) {
                    // Does the post have correct province
                    if ( has_category( $province, $post ) ) {
                        // Print your dealer info here. Ideally you would use get_template_part() function
                        // To avoid duplication
                    }
                }
            }
        }
    } else {
        // Loop your posts
        foreach  ($posts as $post ) {
            // Doest the post have correct category
            if ( has_category( $category, $post ) ) {
                // Print your dealer info here. Ideally you would use get_template_part() function
                // To avoid duplication
            }
        }
    }
}

SO网友:user2812779

我解决了这个问题,下面是使它对我起作用的代码:

<?php
$prev_country = \'\';
$prev_province = \'\';
$netherlands = \'<div class="col-lg-12"><h2 class="dealer-country-title">netherlands</h2></div>\';
/*  Start While loop */
while ( have_posts() ): the_post();
    $current_country = get_field( \'country\' );
    $current_province = get_field( \'province\' );
    if ( $current_country != $prev_country ) {
        if($current_country == "1netherlands") {
            echo $netherlands;
        } else 
        {
      echo \'<div class="col-lg-12"><h2 class="dealer-country-title">\' .  $current_country . \'</h2></div>\';
    }
    }
    if ( $current_province != $prev_province ) {
      echo \'<div class="col-lg-12"><h4>\' . $current_province . \'</h4></div>\';
    }
    ?>
<div class="col-lg-4">
    <div class="dealer-title"><?php the_title(); ?></div>
    <div class="dealer-city"><?php the_field(\'city\'); ?></div>
    <div class="dealer-phone"><?php the_field(\'phone\'); ?></div>
    <div class="dealer-site"><?php the_field(\'site\'); ?></div>
    <div class="dealer-email"><?php the_field(\'email\'); ?></div>
</div>
<?php
    $prev_country = $current_country; // update the vars
    $prev_province = $current_province;

endwhile; ?>

结束

相关推荐

如何测试unctions.php中的类(来自插件)

我为函数添加了各种代码更改。php。其中许多依赖于某些活动插件,因为它们引用这些插件定义的类。如果插件被禁用,将导致500服务器错误,因为找不到相应的类。有没有办法先考一门课?例如,以下函数依赖于WooCommerce扩展中的类。// Add prices to variations add_filter( \'woocommerce_variation_option_name\', \'display_price_in_variation_option_name\' ); func