Display all post type with category as section header

[ad_1]

I need some help. I can’t get my code correct.

Currently I have a custom post type called `solutions`, `services` and `companies`.

In the `solutions` post type, I have created an ACF relationship field called `related_services` that pulls from the `services` custom post type.

In the `companies` post type, I created an ACF relationship field called `company_services` that pulls from the `services` custom post type.

# What it’s suppose to do:

If user is on a specific solutions page, it will list all the services available within that solution. It also checks if the company has that service available. If so, a checkmark will be placed next to it.

# What it’s not doing:

Breaking out the services by category (service\_type). This is where I don’t get how to write the PHP code.

# Result should look like this:

**Solution’s Sub Cat 1**

* Service 1
* Service 2
* Service 3

**Solution’s Sub Cat 2**

* Service 4
* Service 5
* Service 6

# What results look like

**Solution’s Sub Cat 1**

* Service 1
* Service 2
* Service 3
* Service 4
* Service 5
* Service 6

**Solution’s Sub Cat 2**

* Service 1
* Service 2
* Service 3
* Service 4
* Service 5
* Service 6

Please help!

ob_start();

$queried_object = get_queried_object();

if ( $queried_object ) {
$post_id = get_the_ID();
$user_id = get_current_user_id();
$company = get_field(‘user_company’, ‘user_’ . $user_id);
$solution_services = get_field(‘related_services’, $post_id); // get services
$company_services = get_field(‘company_services’, $company->ID); // get company services
$service_taxonomy = get_field(‘related_services’);

//start the counter outside of the loop
$the_increment = 0;

if( $service_taxonomy ) {
foreach( $service_taxonomy as $taxonomy ) {
$terms = get_the_terms( $taxonomy->ID , ‘service_type’ );
foreach ( $terms as $term ) {

echo $term->name;
echo ‘<div class=”emp-service-accordion”>’;

foreach ($solution_services as $key => $value) { // assign each services as $value
$the_increment = $the_increment + 1;
$exp_post = get_post( $value );
$has_service = false;

foreach ($company_services as $key => $user_value) {
if ($user_value == $value) { // if company service == $value, that means they have service
$has_service = true;
}
} // Build a breakout point here to intermix html and php
?>
<?php the_post(); ?>
<div class=”service-item<?php echo ($has_service) ? ” service-item–active” : null; ?>”>
<input class=”service-checkbox” type=”checkbox” name=”collapse<?php echo $the_increment; ?>” id=”service-<?php echo $the_increment; ?>” />
<label class=”service-label” for=”service-<?php echo $the_increment; ?>”>
<span class=”service-caret”>
<svg width=”25″ height=”22″ viewBox=”0 0 25 22″ fill=”none” xmlns=”http://www.w3.org/2000/svg”>
<path d=”M16.9907 7.88232L12.3739 12.0698L7.7571 7.88232L6.33887 9.17147L12.3739 14.6572L18.4089 9.17147L16.9907 7.88232Z” fill=”#1D4F91″/>
</svg>
</span>
<span class=”service-title”>
<?php echo $exp_post->post_title; ?>
<span class=”service-active”>
<svg width=”24″ height=”24″ viewBox=”0 0 24 24″ fill=”none” xmlns=”http://www.w3.org/2000/svg”>
<path fill-rule=”evenodd” clip-rule=”evenodd” d=”M3.5 12C3.5 7.308 7.308 3.5 12 3.5C16.692 3.5 20.5 7.308 20.5 12C20.5 16.692 16.692 20.5 12 20.5C7.308 20.5 3.5 16.692 3.5 12ZM6.6 12.2948L10.4388 16.1335L17.3486 9.22376L16.266 8.13354L10.4388 13.9608L7.68253 11.2122L6.6 12.2948Z” fill=”#007A3B”/>
</svg>
</span>
</span>
</label>
<div class=”service-content”>
<div class=”post-excerpt”><?php echo $exp_post->post_excerpt; ?></div>
</div>
</div>
<?php }
echo ‘</div>’;
}
}
}

echo ‘</div>’;

}

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer