Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Filters: Add taxonomy term counts #228

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions source/wp-content/themes/wporg-showcase-2022/inc/block-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ function ( $a, $b ) {
_n( 'Popular tags <span>%s</span>', 'Popular tags <span>%s</span>', $count, 'wporg' ),
$count
);
$option_labels = get_option_labels( $tags );
return array(
'label' => $label,
'title' => __( 'Popular tags', 'wporg' ),
'key' => 'tag',
'action' => home_url( '/archives/' ),
'options' => array_combine( wp_list_pluck( $tags, 'slug' ), wp_list_pluck( $tags, 'name' ) ),
'options' => array_combine( wp_list_pluck( $tags, 'slug' ), $option_labels ),
'selected' => $selected,
);
}
Expand All @@ -101,12 +102,13 @@ function get_flavor_options( $options ) {
_n( 'Flavors <span>%s</span>', 'Flavors <span>%s</span>', $count, 'wporg' ),
$count
);
$option_labels = get_option_labels( $flavors );
return array(
'label' => $label,
'title' => __( 'Flavors', 'wporg' ),
'key' => 'flavor',
'action' => home_url( '/archives/' ),
'options' => array_combine( wp_list_pluck( $flavors, 'slug' ), wp_list_pluck( $flavors, 'name' ) ),
'options' => array_combine( wp_list_pluck( $flavors, 'slug' ), $option_labels ),
'selected' => $selected,
);
}
Expand Down Expand Up @@ -147,12 +149,14 @@ function get_category_options( $options ) {
_n( 'Categories <span>%s</span>', 'Categories <span>%s</span>', $count, 'wporg' ),
$count
);
$option_labels = get_option_labels( $categories );

return array(
'label' => $label,
'title' => __( 'Categories', 'wporg' ),
'key' => 'cat',
'action' => home_url( '/archives/' ),
'options' => array_combine( wp_list_pluck( $categories, 'term_id' ), wp_list_pluck( $categories, 'name' ) ),
'options' => array_combine( wp_list_pluck( $categories, 'term_id' ), $option_labels ),
'selected' => $selected,
);
}
Expand Down Expand Up @@ -399,3 +403,22 @@ function update_site_breadcrumbs( $breadcrumbs ) {

return $breadcrumbs;
}

/**
* Get an array of option labels.
* Each label is a string formatted as 'name (count)'. e.g, Business (30).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

*
* @param array $taxonomy An arrays with taxonomy terms. e.g., Categories = [ Business, Store, etc. ].
* @return array An array of formatted option label strings.
*/
function get_option_labels( $taxonomy ) {
$option_labels = array_map(
function( $name, $count ) {
return $name . " ($count)";
},
wp_list_pluck( $taxonomy, 'name' ),
wp_list_pluck( $taxonomy, 'count' )
);

return $option_labels;
}
Loading