$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - WordPress alphabetical A-Z custom post type post result display|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin11PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far