0

I want to create an alphabetical pagination for terms from custom taxonomy so I'll be displaying it like this:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

So for example if I click on the letter A it would list all terms from custom taxonomy that start with the letter A. How can I do this? I'm not skilled at programming so this is what done so far by taking bits of code from various snippets.

<?php get_header(); ?>

<div id="content-small">
<div class="list-terms">
<h1 class="list-title">Listing all Tags available</h1>

<div class="alphanav">
<a href="<?php bloginfo('url'); ?>/genre/?character=A">A</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=B">B</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=C">C</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=D">D</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=E">E</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=F">F</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=G">G</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=H">H</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=I">I</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=J">J</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=K">K</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=L">L</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=M">M</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=N">N</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=O">O</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=P">P</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=Q">Q</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=R">R</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=S">S</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=T">T</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=U">U</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=V">V</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=W">W</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=X">X</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=Y">Y</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=Z">Z</a>
<a href="<?php bloginfo('url'); ?>/genre/?character=#">#</a>
  </div>

<?php $args = array('name__like' => "a", 'order' => 'ASC');
    $terms = get_terms('post_tag', $args);
    if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
        $count = count($terms);
        $i=0;
        $term_list = '<ul class="my_term-archive">';
        echo '<h2 class="term-letter">A</h2>';
        foreach ($terms as $term) {
            $i++;
            $term_list .= '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></li>';
            if ($count != $i) {
                $term_list .= '';
            }
            else {
                $term_list .= '</ul>';
            }
        }
        echo $term_list;
    }
    ?>
     </div>
</div>

<?php get_footer(); ?>

function.php

function the_dramatist_filter_term_clauses( $clauses ) {
    remove_filter('term_clauses','the_dramatist_filter_term_clauses');
    $pattern = '|(name LIKE )\'%(.+%)\'|';
    $clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
    return $clauses;
}
add_filter('terms_clauses','the_dramatist_filter_term_clauses');

So far I only managed to get list of terms starting with A but I haven't been able to get the pagination links working. How can I get the pagination links working and how can I do the rest of the letters without me having to manually hard code each of the letters?

1 Answer 1

1

In your code you are showing "A" for all the paginations and you also don't need to create link for every letters in English. I fixed them. And I also added a class here called selected to current link in pagination link groups by which you can design the current link in pagination group. Please analyze the code first and write your CSS according to it. Here is the updated page code-

<?php 
get_header();
$name_like = isset($_GET['character']) ? $_GET['character'] : '';
$letters = range('A', 'Z');
?>

    <div id="content-small">
        <div class="list-terms">
            <h1 class="list-title">Listing all Tags available</h1>

            <div class="alphanav">
                <?php foreach ($letters as $letter): ?>
                    <?php if (strtoupper($name_like) == strtoupper($letter)):?>
                        <a class='selected' href="<?php bloginfo('url'); ?>/genre/?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
                    <?php else: ?>
                        <a href="<?php bloginfo('url'); ?>/genre/?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
                    <?php endif;?>
                <?php endforeach;?>
                <a href="<?php bloginfo('url'); ?>/genre/?character=#">#</a>
            </div>

            <?php $args = array('name__like' => $name_like, 'order' => 'ASC');
            $terms = get_terms('category', $args);
            if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
                $count = count($terms);
                $i=0;
                $term_list = '<ul class="my_term-archive">';
                echo '<h2 class="term-letter">'. strtoupper($name_like) . '</h2>';
                foreach ($terms as $term) {
                    $i++;
                    $term_list .= '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a></li>';
                    if ($count != $i) {
                        $term_list .= '';
                    }
                    else {
                        $term_list .= '</ul>';
                    }
                }
                echo $term_list;
            }
            ?>
        </div>
    </div>

<?php get_footer(); ?>

And I also think you don't need <a href="<?php bloginfo('url'); ?>/genre/?character=#">#</a> but I kept this in case you need it.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.