admin管理员组文章数量:1130349
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 badges2 Answers
Reset to default 0This 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; ?>
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 badges2 Answers
Reset to default 0This 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; ?>
本文标签: custom post typesShow parent category and subcategory once in while loop
版权声明:本文标题:custom post types - Show parent category and subcategory once in while loop 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749249355a2339432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论