Nick Shaw

Create sold page for the plugin ‘Motors – Car Dealer, Classifieds & Listing’ using Divi

Add this to the functions.php file

add_shortcode('sold_cars', function ($atts) {
    $atts = shortcode_atts([
        'posts_per_page' => 12,
    ], $atts);

    $paged = max(1, get_query_var('paged') ?: get_query_var('page'));

    $query = new WP_Query([
        'post_type'      => 'listings',
        'post_status'    => 'publish',
        'posts_per_page' => intval($atts['posts_per_page']),
        'paged'          => $paged,
        'meta_query'     => [
            [
                'key'     => 'car_mark_as_sold',
                'value'   => 'on',
                'compare' => '=',
            ],
        ],
    ]);

    ob_start();

    if ($query->have_posts()) {
        echo '<div class="sold-cars-grid">';

        while ($query->have_posts()) {
            $query->the_post();

            echo '<div class="sold-car-card">';
            
            if (has_post_thumbnail()) {
                echo '<a href="' . esc_url(get_permalink()) . '">';
                the_post_thumbnail('medium_large');
                echo '</a>';
            }

            echo '<span class="sold-badge">Sold</span>';
            echo '<h3><a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a></h3>';

           $price = get_post_meta(get_the_ID(), 'price', true);

			if ($price !== '' && $price !== null) {
				if ((float) $price < 0) {
					echo '<p class="sold-car-price">POA</p>';
				} else {
					echo '<p class="sold-car-price">£' . esc_html(number_format((float) $price)) . '</p>';
				}
			}

            echo '</div>';
        }

        echo '</div>';

        echo '<div class="sold-cars-pagination">';
        echo paginate_links([
            'total'   => $query->max_num_pages,
            'current' => $paged,
        ]);
        echo '</div>';
    } else {
        echo '<p>No sold cars found.</p>';
    }

    wp_reset_postdata();

    return ob_get_clean();
});

Add this CSS to the stlyesheet

.sold-cars-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 28px;
}

.sold-car-card {
    position: relative;
    background: #fff;
    border: 1px solid #e5e5e5;
    padding: 16px;
}

.sold-car-card img {
    width: 100%;
    height: auto;
    display: block;
}

.sold-car-card h3 {
    font-size: 20px;
    margin: 16px 0 8px;
}

.sold-badge {
    position: absolute;
    top: 24px;
    left: 24px;
    background: #c00;
    color: #fff;
    padding: 6px 12px;
    font-weight: 700;
    text-transform: uppercase;
    font-size: 13px;
}

.sold-car-price {
    font-weight: 700;
}

.sold-cars-pagination {
    margin-top: 32px;
    text-align: center;
}

@media (max-width: 980px) {
    .sold-cars-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 600px) {
    .sold-cars-grid {
        grid-template-columns: 1fr;
    }
}
Exit mobile version