A
A
Asya2016-10-06 21:43:16
CMS
Asya, 2016-10-06 21:43:16

How to do alphabetical search in WordPress?

I want to make an alphabetical index on the site, where an alphabetical search would be performed. The problem is that all plugins like Alphabetic pagination that I installed don't work for me for some reason. I need the search to be done through the taxonomy and not through the categories. I also tried as it is written here - it didn’t help, it just turned out that when you click on any letter, just an archive with records is displayed. Are there any wordpress plugins for alphabetical search using taxonomy? Or how can I change this code so that I only show posts whose title starts with a certain letter, and not an archive of all posts?

<?php

/*
  Template Name: artists2
 */
get_header();

/* eng */
echo '<ul>';
for ($i = ord('A'); $i <= ord('Z'); $i++) {
    echo '<li><a href="/persons?letter=' . chr($i) . '">' . chr($i) . '</a></li>';  // artists.html - меняем на свой адрес
}
echo '</ul>';

/* rus */
echo '<ul>';
$abc_rus = array();
foreach (range(chr(0xC0), chr(0xDF)) as $b) {
    $abc_rus[] = iconv('CP1251', 'UTF-8', $b);
}
foreach ($abc_rus as $lette_rus) {
    echo '<li><a href="/persons?letter=' . $lette_rus . '">' . $lette_rus . '</a></li>';  // artists.html - меняем на свой адрес
}
echo '</ul>';

/* number */
echo '<ul>';
for ($i = 0; $i <= 9; $i++) {
    echo '<li><a href="/persons?letter=' . $i . '">' . $i . '</a></li>';  // artists.html - меняем на свой адрес
}
echo '</ul>';

$letter = mb_substr($_GET['letter'], 0, 1, 'utf-8');
$letter_upp = mb_strtoupper($letter, 'UTF-8');

// Получаем все категории. ћожно заменить "category" на вашу таксономию.
// Сортируем по названию в порядке убывания. Также не будем показывать категории у которых нет записей.
$terms = get_terms("person_name", 'orderby=name&hide_empty=0&order=ASC');
// Проверяем есть ли у нас на сайте есть категории
$count = count($terms);
if ($count > 0) {
    echo "<ul class='new_singer'>";
    // Перебираем массив со всеми категориями
    foreach ($terms as $term) {
        // Берем первую букву категории
        $artist_name_letter = mb_substr($term->name, 0, 1, 'utf-8');
        // Сравниваем без учета регистра искомую букву с первой у категории
        if (strnatcasecmp($letter_upp, $artist_name_letter) == 0) {
            /* выводим название категории которая подходит */
            echo "<li>" . $term->name . "</a></li>";
        }
    }
    echo "</ul>";
}

get_sidebar();
get_footer();
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
branky, 2018-06-14
@asyaevloeva

On the topic of the alphabetical index for WordPress, I wrote a detailed instruction. It is possible to apply the code in custom taxonomies, as well as use Cyrillic as alphabetic indexes.
WordPress Index

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question