php - Wordpress loop add heading before first of type

admin2025-06-06  8

Confusing title but using the Types toolset I added the two letter state code (CA, TX, FL, etc...) to each of my 'Store' custom post type. The following query will order all my stores by state code, in desc/asc order:

$state_code = types_render_field("state-code", array());

$args = array( 
    'post_type' => 'store',
    'posts_per_page' => 100, 
    'orderby' => array( 'ASC' => 'DESC', 'meta_value' => 'ASC' ),
    'meta_key' => 'wpcf-state-code',
);

My question: if I have 10 stores in California, is there a way that, before the first store, I print out a heading (in this case, California)? So if i have states in CA, TX, and FL, before my block of stores in CA it'll say 'Califoarnia', before my block of stores in FL it'll say 'Florida', etc...?

And if so, is there a way to add a dynamic class to each heading? Something like <h2 class="<?php echo $state_code; ?>-state">California</h2> which would output ca-state as the class?

Confusing title but using the Types toolset I added the two letter state code (CA, TX, FL, etc...) to each of my 'Store' custom post type. The following query will order all my stores by state code, in desc/asc order:

$state_code = types_render_field("state-code", array());

$args = array( 
    'post_type' => 'store',
    'posts_per_page' => 100, 
    'orderby' => array( 'ASC' => 'DESC', 'meta_value' => 'ASC' ),
    'meta_key' => 'wpcf-state-code',
);

My question: if I have 10 stores in California, is there a way that, before the first store, I print out a heading (in this case, California)? So if i have states in CA, TX, and FL, before my block of stores in CA it'll say 'Califoarnia', before my block of stores in FL it'll say 'Florida', etc...?

And if so, is there a way to add a dynamic class to each heading? Something like <h2 class="<?php echo $state_code; ?>-state">California</h2> which would output ca-state as the class?

Share Improve this question asked Dec 15, 2016 at 4:53 VCPVCP 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You could try a concept like the following (post your loop and I will improve the model):

$state_group = 'Open Loop';

// start the loop
// if posts then while

$state_code = // lookup this post state code
if ( $state_code != $state_group ){
    echo $state_code; 
    $state_group = $state_code;
}

//end loop
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749207114a317259.html

最新回复(0)