skip to Main Content

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.

2

Answers


  1. 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 have styles for the disabled class
    Login or Signup to reply.
  2. 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";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search