我想通過 PHP 在 wordpress 上創建一個詞匯表概述頁面,可以通過簡碼使用。我希望始終顯示首字母,然后顯示以此開頭的各個主題(帖子)。
例子:
一種
- 蘋果
- 杏
乙
- 香蕉
- 黑莓
等等...
為了實作這一點,我使用以下代碼:
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
array(
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
));
echo '<ul>';
if( $all_posts->have_posts()){
foreach( range( 'A', 'Z' ) as $letter ) {
echo '<div ><div >' . $letter. '</div>';
while( $all_posts->have_posts() ){
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper( substr( $title, 0, 1 ) );
if( $initial == $letter ){
echo '<li><a href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
到目前為止它有效,但現在它顯示沒有帖子的字母。這就是現在的樣子
我試圖用 if 查詢來做到這一點,但到目前為止,我被卡住了。有人能幫我嗎?最好的問候,謝謝!
uj5u.com熱心網友回復:
使用 PHP sort() 函式對陣列進行排序,然后回圈遍歷結果
<?PHP
$list=['apples','popsicles','Zinger','donkeys','bananas','joe',
'Locusts','gazelles','Angels','Popsicle','Dongle','jump','cocoa'
];
//convert all elements to same case
//sorting will sort by case
$list =array_map('strtolower', $list);
//sort the array
sort($list);
$last_letter=null;
foreach($list as $item){
$current_letter=substr($item,0,1);
if($last_letter!=$current_letter){
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$current_letter?>
</div>
<?php
$last_letter=$current_letter;
}
?>
<div style="margin:1rem;padding:1rem;background:#f5f5f5;">
<?=$item?>
</div>
<?PHP
}
?>
uj5u.com熱心網友回復:
我確信除了在while回圈中運行 26 次之外還有更好的解決方案。無論如何,這就是你要找的東西。
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
if ($all_posts->have_posts()) {
foreach (range('A', 'Z') as $letter) {
$foundPostable = false;
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial === $letter) {
if ($foundPostable === false) {
$foundPostable = true;
echo '<div ><div >' . $letter. '</div>';
}
echo '<li><a href="/glossar/'. $name . '">' . $title . '</a></li>';
}
}
$all_posts->rewind_posts();
}
}
echo '</ul>';
}
add_shortcode( 'glossary', 'glossary' );
至于改進,這樣的事情也可能奏效。
// get glossary
function glossary($post_id) {
$all_posts = new WP_Query(
[
'posts_per_page' => -1,
'post_type' => 'glossar',
'orderby' => 'title',
'order' => 'ASC',
]
);
echo '<ul>';
$startLetter = '';
while ($all_posts->have_posts()) {
$all_posts->the_post();
$title = get_the_title();
$name = get_post_field( 'post_name', get_post() );
$initial = strtoupper(substr($title, 0, 1));
if ($initial !== $startLetter) {
$startLetter = $initial
echo '<div ><div >' . $letter . '</div>';
}
echo '<li><a href="/glossar/'. $name . '">' . $title . '</a></li>';
}
echo '</ul>';
}
add_shortcode('glossary', 'glossary');
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/471050.html
