我有书籍作者的自定义分类。我需要使用字母表中的字母创建索引,当用户点击字母时,索引下方会出现一个列表,其中所有书籍作者的姓氏都以该字母开头。我得到了一切可以很好地工作,除了它会列出在该术语中任何地方找到该字母的所有条款,而不仅仅是第一个字母:
<?php global $product;
$name_like = isset($_GET['character']) ? $_GET['character'] : '';
$letters = range('A', 'Z');
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$slug = $term->slug;
?>
<div id="content-small">
<div class="list-terms">
<div class="alphanav">
<?php foreach ($letters as $letter): ?>
<?php if (strtoupper($name_like) == strtoupper($letter)):?>
<a class='selected' href="<?php bloginfo('url'); ?>/auteurs/<?php echo $slug ?>?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
<?php else: ?>
<a href="<?php bloginfo('url'); ?>/auteurs/<?php echo $slug ?>?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>
<?php endif;?>
<?php endforeach;?>
<a class="selected set-height" href="<?php bloginfo('url'); ?>/auteurs/<?php echo $slug ?>?character=#">#</a>
</div>
<?php
$taxonomy = 'auteurs';
$queried_term = get_query_var($taxonomy);
$args = array(
'name__like' => $name_like,
'orderby' => 'name',
'order' => 'ASC',
);
$terms = get_terms($taxonomy, $args, 'slug='.$queried_term);
if ($terms) {
$count = count($terms);
$i=0;
$term_list = '<div class="tab-auteur">
<input id="tab-one-auteur" type="checkbox" name="tabs-auteur">
<label for="tab-one-auteur">La Liste</label>
<ul class="bio-list tab-content-auteur">';
//echo '<h2 class="term-letter">'. strtoupper($name_like) . '</h2>';
foreach($terms as $term) {
$names = $term->name;
$unwanted_array = array( 'Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
$firstname = substr($names, strpos($names, ",") + 1);
$arr = explode(",", $names, 2);
$lastname = $arr[0];
$lastname = strtr( $lastname, $unwanted_array );
$lastname = strtoupper( $lastname );
$comma = ',';
$whichname = strpos($names, $comma);
if ($whichname === false) {
$full = strtoupper( $names );
} else {
$full = $lastname . ', ' . $firstname;
}
$i++;
$term_list .= '<li><a href="'.get_term_link($term->slug,
$taxonomy).'?character='. $name_like .'">' . $full . '</a></li>';
if ($count != $i) {
$term_list .= '';
}
else {
$term_list .= '</ul></div>';
}
}
echo $term_list;
}
else {
echo '<ul class="bio-list">PAS DE AUTEURS</ul>';
}
?>
</div>
我研究了很多,发现在较新版本的Wordpress中,“name_like”的行为发生了变化。在post的帮助下,我使用term_clauses钩子来过滤我的functions.php文件中的查询:
function llps_filter_term_clauses( $clauses ) {
remove_filter('term_clauses','llps_filter_term_clauses');
$pattern = '|(name LIKE )\'%(.+%)\'|';
$clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
return $clauses;
}
add_filter('terms_clauses','llps_filter_term_clauses');
并且它有效 - 我确定它确实......直到我下次登录项目。它不再起作用,我不能为我的生活找出原因!
答案 0 :(得分:0)
我有同样的问题。在最新的WP版本中可能会发生一些变化。我在4.9.1
中遇到了这个问题这是我修复它的方法,我改变了这个:
$pattern = '|(name LIKE )\'%(.+%)\'|';
进入这个:
$pattern = '|(name LIKE )\'{.*?}(.+{.*?})\'|';
如果你想知道为什么我这样做,我建议在preg_replace之前和之后临时打印$子句。我的意思是你的代码中有这样的东西:
function llps_filter_term_clauses( $clauses ) {
echo '<pre>';print_r($clauses);echo "\n";
remove_filter('term_clauses','llps_filter_term_clauses');
$pattern = '|(name LIKE )\'%(.+%)\'|';
$clauses['where'] = preg_replace($pattern,'$1 \'$2\'',$clauses['where']);
print_r($clauses);die();
return $clauses;
}
你应该看到典型的&#34;%&#34;在sql LIKE运算符中由WP转换为类似:
{be8aacdf46dd49d9036fd16c13613bf3036378f188b3535c3416ae7d88844d20} 你的来信 {be8aacdf46dd49d9036fd16c13613bf3036378f188b3535c3416ae7d88844d20}
即。 {}内的一个字符串,这就是我更改过滤器中的正则表达式的原因。希望这会有所帮助。
答案 1 :(得分:0)
实际上,您可以创建自定义参数,而不是替换where子句
例如:
add_filter( 'terms_clauses', 'terms_clauses_47840519', 10, 3 );
function terms_clauses_47840519( $clauses, $taxonomies, $args ){
global $wpdb;
if( !isset( $args['__first_letter'] ) ){
return $clauses;
}
$clauses['where'] .= ' AND ' . $wpdb->prepare( "t.name LIKE %s", $wpdb->esc_like( $args['__first_letter'] ) . '%' );
return $clauses;
}
并使用
之类的名称进行调用$terms = get_terms( array(
'taxonomy' => $taxonomy,
'__first_letter' => 'a', // desired first letter
) );