$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Wordpress loop add heading before first of type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Wordpress loop add heading before first of type

matteradmin8PV0评论

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
Post a comment

comment list (0)

  1. No comments so far