0 php wordpress filter alphabetical custom-taxonomy
我有一个针对书籍作者的自定义分类法。我需要使用字母表字母创建一个索引,当用户单击某个字母时,索引下方会出现一个列表,其中包含姓氏以该字母开头的所有图书作者。我让一切都工作得很好,除了它会列出在术语中任何地方找到该字母的所有术语,而不仅仅是第一个字母:
\n\n<?php global $product;\n\n$name_like = isset($_GET[\'character\']) ? $_GET[\'character\'] : \'\';\n$letters = range(\'A\', \'Z\');\n$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) ); \n $slug = $term->slug;\n?>\n\n<div id="content-small">\n <div class="list-terms">\n\n <div class="alphanav">\n <?php foreach ($letters as $letter): ?>\n <?php if (strtoupper($name_like) == strtoupper($letter)):?>\n <a class=\'selected\' href="<?php bloginfo(\'url\'); ?>/auteurs/<?php echo $slug ?>?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>\n <?php else: ?>\n <a href="<?php bloginfo(\'url\'); ?>/auteurs/<?php echo $slug ?>?character=<?php echo strtoupper($letter)?>"><?php echo strtoupper($letter)?></a>\n <?php endif;?>\n <?php endforeach;?>\n <a class="selected set-height" href="<?php bloginfo(\'url\'); ?>/auteurs/<?php echo $slug ?>?character=#">#</a>\n </div>\n<?php\n\n$taxonomy = \'auteurs\';\n$queried_term = get_query_var($taxonomy);\n$args = array(\n\'name__like\' => $name_like,\n \'orderby\' => \'name\',\n \'order\' => \'ASC\',\n );\n\n$terms = get_terms($taxonomy, $args, \'slug=\'.$queried_term);\n\nif ($terms) {\n $count = count($terms);\n $i=0;\n $term_list = \'<div class="tab-auteur">\n <input id="tab-one-auteur" type="checkbox" name="tabs-auteur">\n <label for="tab-one-auteur">La Liste</label>\n <ul class="bio-list tab-content-auteur">\';\n //echo \'<h2 class="term-letter">\'. strtoupper($name_like) . \'</h2>\';\n\n foreach($terms as $term) {\n $names = $term->name;\n $unwanted_array = array( \'\xc5\xa0\'=>\'S\', \'\xc5\xa1\'=>\'s\', \'\xc5\xbd\'=>\'Z\', \'\xc5\xbe\'=>\'z\', \'\xc3\x80\'=>\'A\', \'\xc3\x81\'=>\'A\', \'\xc3\x82\'=>\'A\', \'\xc3\x83\'=>\'A\', \'\xc3\x84\'=>\'A\', \'\xc3\x85\'=>\'A\', \'\xc3\x86\'=>\'A\', \'\xc3\x87\'=>\'C\', \'\xc3\x88\'=>\'E\', \'\xc3\x89\'=>\'E\',\n \'\xc3\x8a\'=>\'E\', \'\xc3\x8b\'=>\'E\', \'\xc3\x8c\'=>\'I\', \'\xc3\x8d\'=>\'I\', \'\xc3\x8e\'=>\'I\', \'\xc3\x8f\'=>\'I\', \'\xc3\x91\'=>\'N\', \'\xc3\x92\'=>\'O\', \'\xc3\x93\'=>\'O\', \'\xc3\x94\'=>\'O\', \'\xc3\x95\'=>\'O\', \'\xc3\x96\'=>\'O\', \'\xc3\x98\'=>\'O\', \'\xc3\x99\'=>\'U\',\n \'\xc3\x9a\'=>\'U\', \'\xc3\x9b\'=>\'U\', \'\xc3\x9c\'=>\'U\', \'\xc3\x9d\'=>\'Y\', \'\xc3\x9e\'=>\'B\', \'\xc3\x9f\'=>\'Ss\', \'\xc3\xa0\'=>\'a\', \'\xc3\xa1\'=>\'a\', \'\xc3\xa2\'=>\'a\', \'\xc3\xa3\'=>\'a\', \'\xc3\xa4\'=>\'a\', \'\xc3\xa5\'=>\'a\', \'\xc3\xa6\'=>\'a\', \'\xc3\xa7\'=>\'c\',\n \'\xc3\xa8\'=>\'e\', \'\xc3\xa9\'=>\'e\', \'\xc3\xaa\'=>\'e\', \'\xc3\xab\'=>\'e\', \'\xc3\xac\'=>\'i\', \'\xc3\xad\'=>\'i\', \'\xc3\xae\'=>\'i\', \'\xc3\xaf\'=>\'i\', \'\xc3\xb0\'=>\'o\', \'\xc3\xb1\'=>\'n\', \'\xc3\xb2\'=>\'o\', \'\xc3\xb3\'=>\'o\', \'\xc3\xb4\'=>\'o\', \'\xc3\xb5\'=>\'o\',\n \'\xc3\xb6\'=>\'o\', \'\xc3\xb8\'=>\'o\', \'\xc3\xb9\'=>\'u\', \'\xc3\xba\'=>\'u\', \'\xc3\xbb\'=>\'u\', \'\xc3\xbd\'=>\'y\', \'\xc3\xbe\'=>\'b\', \'\xc3\xbf\'=>\'y\' );\n$firstname = substr($names, strpos($names, ",") + 1); \n$arr = explode(",", $names, 2);\n$lastname = $arr[0]; \n$lastname = strtr( $lastname, $unwanted_array ); \n$lastname = strtoupper( $lastname );\n$comma = \',\';\n$whichname = strpos($names, $comma);\n\nif ($whichname === false) {\n$full = strtoupper( $names );\n} else {\n$full = $lastname . \', \' . $firstname;\n}\n\n$i++;\n $term_list .= \'<li><a href="\'.get_term_link($term->slug, \n$taxonomy).\'?character=\'. $name_like .\'">\' . $full . \'</a></li>\';\n if ($count != $i) {\n $term_list .= \'\';\n }\n else {\n $term_list .= \'</ul></div>\';\n }\n }\n echo $term_list;\n\n }\nelse {\n echo \'<ul class="bio-list">PAS DE AUTEURS</ul>\';\n }\n ?>\n </div> \nRun Code Online (Sandbox Code Playgroud)\n\n我进行了大量研究,发现 \xe2\x80\x9cname_like\xe2\x80\x9d 的行为在较新版本的 WordPress 中发生了变化。在这篇文章的帮助下,我使用 term_clauses 钩子来过滤 function.php 文件中的查询:
\n\nfunction llps_filter_term_clauses( $clauses ) {\n remove_filter(\'term_clauses\',\'llps_filter_term_clauses\');\n $pattern = \'|(name LIKE )\\\'%(.+%)\\\'|\';\n $clauses[\'where\'] = preg_replace($pattern,\'$1 \\\'$2\\\'\',$clauses[\'where\']);\n return $clauses;\n}\n\nadd_filter(\'terms_clauses\',\'llps_filter_term_clauses\');\nRun Code Online (Sandbox Code Playgroud)\n\n它有效 - 我\xe2\x80\x99m确信它有效\xe2\x80\xa6,直到我下次登录该项目。它\xe2\x80\x99s不再工作了,我可以\xe2\x80\x99t用我的一生找出原因!
\n实际上,您可以创建自定义参数而不是替换 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;
}
Run Code Online (Sandbox Code Playgroud)
并用类似的东西来称呼它
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'__first_letter' => 'a', // desired first letter
) );
Run Code Online (Sandbox Code Playgroud)