Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP - WOO | wp_query loop alphabetically sorted list with first letter before each
// Originally used inside /woocommerce/ folder in my child theme to display a product_category terms list.
// Displays terms separated by first letter and before each series inserts the main letter, like a index.
// Settings order by title, only showing child subcategories of those terms that have posts assigned.
// Place in your page-template.php and customise the arguments to fit your needs.
$args = array(
'taxonomy' => 'product_cat',
'hide_empty' => true,
'posts_per_page' => 300,
'parent' => 77,
'orderby' => 'title',
);
$something-semantic = get_terms($args);
foreach($something-semantic as $something-semantic) {
$d_name = $something-semantic->name;
if (($d_name[0] !== $prev_letter2) && ($d_name[0] !== $prev_letter) && ( !is_null($prev_letter))) {
echo '</ul></div>';
}
if ($d_name[0] !== $prev_letter) {
echo '<div class="main-letter-title"><h2>' . $d_name[0] . "</h2>";
$prev_letter2 = $prev_letter;
$prev_letter = $d_name[0];
echo '<ul class="terms-list">';
}
echo '<li class="term-item"><a href="' . esc_url( get_term_link( $something-semantic ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $d_name ) ) . '">' . $d_name . '</a></li>';
$d_index++;
}
echo '</ul></div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment