0

How to display WordPress custom post type result alphabetical letters or pagination? for a example i have a post type called "homeless". under homeless there are 50 states (categories). there are 259 listing under Arkansas. i want to show a alphabetical letters a-z in the Arkansas category. for a example if someone click on letter "B" it will show all listing under "Arkansas" begin with letter"B". can anyone have a suggestion or php code for this?. Please see the image . thank you and kind regards. enter image description here

2

Another option if you don't want to hard-code the alphabet links (i.e., if no posts start with a letter, then that letter is not shown in pagination at all).

// Always show pagination
// First, grab all posts.
$posts = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'homeless',
    'orderby' => 'title',
    'order' => 'ASC'
));
// Next, grab the first letter of each title.
$firstLetters = array();
foreach($posts as $post) {
    $title = $post->post_title;
    $startingLetter = substr($title, 0, 1);
    $dupeFirstLetters[] = $startingLetter;
    // Remove duplicates
    $firstLetters = array_unique($dupeFirstLetters);
    // Alphabetize
    sort($firstLetters);
}
foreach($firstLetters as $letter) {
    // Output the letter pagination, only for letters that have posts
    echo "<a href=\"?letter=$letter\">$letter</a>";
}
// If there is a request for a specific "letter" in the query string
if(!empty($_GET['letter'])) {
    $letter = $_GET['letter'];
}
// Else, default to showing the first found letter
// i.e. A if there are any post titles starting with A
else {
    $letter = $firstLetters[0];
}
// Finally, run a custom query to display the posts - see Max's link #1 above for specifics

You can then run the query to pull just the posts starting with that letter, and display them however you want.

  • WebElaine thank you for the quick reply . this is a best way to start my project. thank you very much for the help.. :) – Gayal cham Mar 9 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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