我想为自定义分类法中的术语创建字母顺序分页,因此我将按如下方式显示它:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
因此,例如,如果我单击字母A,它将列出自定义分类法中以字母A开头的所有术语。如何做到这一点?我不擅长编程,所以这是迄今为止通过从各种代码片段中提取一些代码来完成的。
<?php get_header(); ?>
<div id="content-small">
<div class="list-terms">
<h1 class="list-title">Listing all Tags available</h1>
<div class="alphanav">
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=A">A</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=B">B</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=C">C</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=D">D</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=E">E</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=F">F</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=G">G</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=H">H</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=I">I</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=J">J</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=K">K</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=L">L</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=M">M</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=N">N</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=O">O</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=P">P</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=Q">Q</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=R">R</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=S">S</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=T">T</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=U">U</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=V">V</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=W">W</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=X">X</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=Y">Y</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=Z">Z</a>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=#">#</a>
</div>
<?php $args = array(\'name__like\' => "a", \'order\' => \'ASC\');
$terms = get_terms(\'post_tag\', $args);
if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
$count = count($terms);
$i=0;
$term_list = \'<ul class="my_term-archive">\';
echo \'<h2 class="term-letter">A</h2>\';
foreach ($terms as $term) {
$i++;
$term_list .= \'<li><a href="\' . get_term_link( $term ) . \'" title="\' . sprintf(__(\'View all post filed under %s\', \'my_localization_domain\'), $term->name) . \'">\' . $term->name . \'</a></li>\';
if ($count != $i) {
$term_list .= \'\';
}
else {
$term_list .= \'</ul>\';
}
}
echo $term_list;
}
?>
</div>
</div>
<?php get_footer(); ?>
功能。php
function the_dramatist_filter_term_clauses( $clauses ) {
remove_filter(\'term_clauses\',\'the_dramatist_filter_term_clauses\');
$pattern = \'|(name LIKE )\\\'%(.+%)\\\'|\';
$clauses[\'where\'] = preg_replace($pattern,\'$1 \\\'$2\\\'\',$clauses[\'where\']);
return $clauses;
}
add_filter(\'terms_clauses\',\'the_dramatist_filter_term_clauses\');
到目前为止,我只成功地获得了以A开头的术语列表,但我还无法获得分页链接。如何才能使分页链接正常工作,如何在不必手动硬编码每个字母的情况下处理其余字母?
最合适的回答,由SO网友:CodeMascot 整理而成
在代码中,所有分页都显示“A”,并且不需要为每个英文字母创建链接。我把它们修好了。我还添加了一个类,名为selected
到分页链接组中的当前链接,通过该链接可以设计分页组中的当前链接。请先分析代码并根据它编写CSS。以下是更新的页面代码-
<?php
get_header();
$name_like = isset($_GET[\'character\']) ? $_GET[\'character\'] : \'\';
$letters = range(\'A\', \'Z\');
?>
<div id="content-small">
<div class="list-terms">
<h1 class="list-title">Listing all Tags available</h1>
<div class="alphanav">
<?php foreach ($letters as $letter): ?>
<?php if (strtoupper($name_like) == strtoupper($letter)):?>
<a class=\'selected\' href="<?php bloginfo(\'url\'); ?>/genre/?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
<?php else: ?>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
<?php endif;?>
<?php endforeach;?>
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=#">#</a>
</div>
<?php $args = array(\'name__like\' => $name_like, \'order\' => \'ASC\');
$terms = get_terms(\'category\', $args);
if ( !empty( $terms ) && !is_wp_error( $terms ) ) {
$count = count($terms);
$i=0;
$term_list = \'<ul class="my_term-archive">\';
echo \'<h2 class="term-letter">\'. strtoupper($name_like) . \'</h2>\';
foreach ($terms as $term) {
$i++;
$term_list .= \'<li><a href="\' . get_term_link( $term ) . \'" title="\' . sprintf(__(\'View all post filed under %s\', \'my_localization_domain\'), $term->name) . \'">\' . $term->name . \'</a></li>\';
if ($count != $i) {
$term_list .= \'\';
}
else {
$term_list .= \'</ul>\';
}
}
echo $term_list;
}
?>
</div>
</div>
<?php get_footer(); ?>
我也认为你不需要
<a href="<?php bloginfo(\'url\'); ?>/genre/?character=#">#</a>
但我留着这个以防你需要。
希望这有帮助。