<?php
$title    = $args['title'];
$content  = $args['content'];
$category = $args['category'];

$args = array(
    'post_type'           => 'inventory',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      =>-1,
    'orderby'             => 'ASC',
    'order'               => 'ASC',
    'tax_query'           => array(array(
        'taxonomy'      => 'inventory_categories',
        'field'         => 'slug', //This is optional, as it defaults to 'term_id'
        'terms'         => $category ? $category : '',
        'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
    ))
);

$the_query = new WP_Query( $args );
?>
<section class="section inventory-lifecycle-new">
    <div class="container">
        <div class="text-cont slide-up">
            <h2 class="section-title"><?php echo $title; ?></h2>
            <p class="section-content"><?php echo $content; ?></p>
        </div>

        <?php
if ($the_query->have_posts()) {
    $rows = ['first-row', 'second-row', 'third-row', 'fourth-row'];
    $index = 0;

    echo '<div class="row">';

    while ($the_query->have_posts()) : $the_query->the_post();
        $post_id = get_the_ID();
        $titles = get_the_title();
        $inventory_link = get_field('inventory_link');

        // Start a new column for every 3rd post
        if ($index % 3 == 0) {
            if ($index > 0) echo '</ul></div>'; // Close previous ul & div
            echo '<div class="col-lg-6 col-xl-3">';
            echo '<ul class="' . $rows[$index / 3] . '">';
        }
?>
        <li>
            <span class="left">
                <?php echo esc_html($titles); ?>
            </span>
            <span class="right">
                <a href="<?php echo esc_url($inventory_link); ?>" aria-label="<?php echo $titles;?>">
                    <svg width="39" height="35" viewBox="0 0 39 35" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <rect x="0.835938" y="0.808594" width="37.3414" height="33.5115" rx="4.78736" fill="white"/>
                        <path d="M18.1636 11.624L13.9844 16.0335L15.3609 17.4859L18.5666 14.1035V24.375H20.5136V14.1035L23.7193 17.4859L25.0958 16.0335L20.9166 11.624C20.5515 11.2389 20.0564 11.0225 19.5401 11.0225C19.0238 11.0225 18.5287 11.2389 18.1636 11.624Z" fill="#272626"/>
                    </svg>                                    
                </a>
            </span>
        </li>
<?php
        $index++;
    endwhile;

    echo '</ul></div>'; // Close last ul & div
    echo '</div>'; // Close row

    wp_reset_postdata();
}
?>

    </div>
</section>