categories - WordPress alphabetical A-Z custom post type post result display

admin2025-06-02  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.

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.

Share Improve this question asked Mar 7, 2019 at 14:38 Gayal chamGayal cham 31 silver badge2 bronze badges 2
  • 1. wordpress.stackexchange/questions/131333/…, 2. stackoverflow/questions/13671943/… – Max Yudin Commented Mar 7, 2019 at 16:40
  • Max Yudin Thank you for the good information really appreciated it.:) – Gayal cham Commented Mar 9, 2019 at 7:47
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748830117a314076.html

最新回复(0)