Ecommerce Shopify WordPress Discussion

How to add disabled attribute to empty alphabetical pagination items

I have a custom taxonomy alphabetical pagination to list terms by first letter. function alpha_pagination() { $array = array( 'A' => __('A',''), 'B' => __('B',''), 'C' => __('C',''), 'D' => __('D',''), 'E' => __('E',''), 'F' => __('F','') ); $html = ''; foreach ( $array as $key=>$value ){ $active = ( $order == $key ) ? 'selected' : null; $html .= '<a class="button '.$active.'" href="'.get_permalink().'? sortby='.$key.'">'.$value.'</a>'; } print $html; } When I print the function on the taxonomies list page everything works good but the buttons from a to z are all clickable even if the term is empty. What I'm trying to do is adding the disabled attribute to letters without terms. Any help or suggestion would be great. I have no idea in this
To add the disabled attribute to empty alphabetical pagination items, you need to check if there are any terms for each letter before rendering the link/button iterates through an array of letters check for are any terms beginning with each letter using the get_terms function, and then apply the disabled class. you can do something like this function alpha_pagination() { $letters = array('A', 'B', 'C', 'D', 'E', 'F'); // Extend this array as needed // Fetch all terms in the taxonomy $all_terms = get_terms(array( 'taxonomy' => 'your_taxonomy_name', // change to actual taxonomy name 'hide_empty' => true, )); // Filter terms by first letter $terms_by_letter = []; foreach ($all_terms as $term) { $first_letter = strtoupper($term->name[0]); // Get the first letter of the term name if (!isset($terms_by_letter[$first_letter])) { $terms_by_letter[$first_letter] = []; } $terms_by_letter[$first_letter][] = $term; // Group terms by their first letter } $html = ''; foreach ($letters as $letter) { $active = (isset($_GET['sortby']) && $_GET['sortby'] == $letter) ? 'selected' : ''; $disabled = !isset($terms_by_letter[$letter]) ? 'disabled' : ''; // Check if there are terms for this letter if (!empty($disabled)) { $html .= '<span class="button ' . $active . ' ' . $disabled . '">' . $letter . '</span>'; } else { $html .= '<a class="button ' . $active . '" href="' . get_permalink() . '?sortby=' . $letter . '">' . $letter . '</a>'; } } print $html; } $active variable should be set based on your current sorting CSS should hsvr styles for the disabled class

February 17, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.