我想创建一个从A到Z的索引列表,列出特定自定义帖子类型的所有帖子。这是迄今为止的代码:
<?php
/**
* The Template for displaying archive series.
*
* Theme: Default
*/
get_header(); ?>
<div id="content">
<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(\'paged\' => $paged,\'posts_per_page\' => -1,\'post_type\' => \'series_type\',\'post_status\' => \'publish\',\'orderby\' => \'title\',\'order\' => \'asc\');
$fruits = new WP_Query($args);
if($fruits->have_posts()){
$current_first_letter = \'\';
$t = array();
$s = array();
while($fruits->have_posts()){
$fruits->the_post();
$title = get_the_title();
if(is_string($title)){
$first_letter = strtoupper($title[0]);
if($first_letter != $current_first_letter){
$t[] = $first_letter;
$current_first_letter = $first_letter;
}
}
$s[$first_letter][] = get_the_title();
}
}
$t = array_unique($t);
$tc = count($t);
$sc = count($s);
for($i=0; $i<$tc; $i++){
?>
<div>
<h4><?php echo $t[$i]; ?></h4>
<ul>
<?php
foreach($s as $key => $value){
if($key == $t[$i]){
$vc = count($value);
for($j=0; $j<$vc; $j++){
?>
<li><?php echo $value[$j]; ?></li>
<?php
}
}
}
?>
</ul>
</div>
<?php
}
if($fruits->max_num_pages > 1){ ?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( \'Older Entries\', $fruits->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( \'Newer Entries\' ); ?>
</div>
</nav>
<?php } ?>
<?php wp_reset_postdata(); ?>
</div><!-- #content -->
<?php get_footer(); ?>
问题是我不知道如何将它们全部制作成超链接,而不是原始文本。有人能帮我修复代码吗?
最合适的回答,由SO网友:Elex 整理而成
好的,你只需要用首字母检查做一个循环。WordPress查询将处理post_title
ASC
顺序
EDIT : 我在循环中使用get\\u permalink()来获取URL(https://developer.wordpress.org/reference/functions/get_permalink/)
$series = new WP_Query(array(
\'posts_per_page\' => -1,
\'post_type\' => \'series_type\',
\'orderby\' => \'title\',
\'order\' => \'asc\'
));
if($series->have_posts())
{
$letter = \'\';
while($series->have_posts())
{
$series->the_post();
// Check the current letter is the same that the first of the title
if($letter != strtoupper(get_the_title()[0]))
{
echo ($letter != \'\') ? \'</ul></div>\' : \'\';
$letter = strtoupper(get_the_title()[0]);
echo \'<div><ul><h4>\'.strtoupper(get_the_title()[0]).\'</h4>\';
}
echo \'<li><a href="\'.get_permalink().\'">\'.get_the_title().\'</a></li>\';
}
}