custom post types - Show parent category and subcategory once in while loop

admin2025-06-07  64

I have tried several things but unfortunately do not manage to do it in a good way.

I use a while loop to pick up dealers (custom post type) from different countries and provinces. In this loop of dealers I want to categorize them in countries and provinces.

The main category of a dealer is a country and then there is the possibility for some countries to select a subcategory, the province.

In this way I want to categorize them: Categorized dealers

As I said, I have tried several things but it does not work exactly as I want. In addition, it is also too much code in my opinion.

Code:

<?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; ?>

This code only does the main category (countries) but not the subcategory if there is one. That said it does not work well either.

I know that there must be an easier way to realize this. Someone who can steer me in the right direction?

I have tried several things but unfortunately do not manage to do it in a good way.

I use a while loop to pick up dealers (custom post type) from different countries and provinces. In this loop of dealers I want to categorize them in countries and provinces.

The main category of a dealer is a country and then there is the possibility for some countries to select a subcategory, the province.

In this way I want to categorize them: Categorized dealers

As I said, I have tried several things but it does not work exactly as I want. In addition, it is also too much code in my opinion.

Code:

<?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; ?>

This code only does the main category (countries) but not the subcategory if there is one. That said it does not work well either.

I know that there must be an easier way to realize this. Someone who can steer me in the right direction?

Share Improve this question asked Oct 17, 2018 at 7:44 user2812779user2812779 1472 silver badges9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

This is untested, but you have do an nested loop where you loop parent categories and child categories. When you find an correct parent you loop the posts and check which posts have the correct category.

<?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
            }
        }
    }
}

I fixed the problem, here is the code that made it work for me:

<?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; ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749249379a317606.html

最新回复(0)