Search code examples
phpwordpresspaginationgroupingcustom-taxonomy

Add disabled attribute to taxonomy's alphabetical pagination items when no terms exist in the group


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.


Solution

  • In your function, you will need to first iterate through all terms in your taxonomy and populate an array of A-Z letters (and potentially any other leading characters not represented by A-Z). For each unique leading character, ensure that value/flag relating to the uppercase version of the character is set to true to indicate that that character has at least one term. You do not need to actually retain all of the terms while building the lookup array.

    Then iterate over the updated array and print your HTML elements with the desired conditional attributes. printf() is a good tool because it minimizes concatenation and allows placeholders to access the same variable in multiple places.

    Be sure to multibyte-safe functions and encode/escape HTML entities to avoid unexpected bugs from fringe-case values.

    Code: (Demo)

    function alpha_pagination() {
        $chars = array_fill_keys(range('A', 'Z'), false);
        foreach (get_terms(['taxonomy' => 'unknown', 'hide_empty' => true]) as $tax) {
            $chars[mb_strtoupper(mb_substr($tax, 0, 1))] = true;
        }
        ksort($chars);
    
        foreach ($chars as $char => $hasTerms) {
            printf(
                '<%1$s class="button%2$s%3$s">%4$s</%1$s>' . "\n",
                $hasTerms ? 'a' : 'span',  // %1$s
                ($_GET['sortBy'] ?? '_') === $char ? ' selected' : '',  // %2$s
                $hasTerms ? '" href="' . get_permalink() . '?' . http_build_query(['sortby' => $char]) : ' disable',  // %3$s
                esc_html($char)  // %4$s
            );
        }
    }
       
    

    Or if you prefer separate printf() calls: (Demo)

    foreach ($chars as $char => $hasTerms) {
        $selected = ($_GET['sortBy'] ?? '_') === $char ? ' selected' : '';
        if ($hasTerms) {
            printf(
                '<a class="button%s" href="%s?%s">%s</a>',
                $selected,
                get_permalink(),
                http_build_query(['sortby' => $char]),
                esc_html($char)
            );
        } else {
            printf(
                '<span class="button disabled%s">%s</span>',
                $selected,
                esc_html($char)
            );
        }
        echo "\n";
    }