我有一个包含名称的表,每个条目的姓氏都需要截断。迈尔斯·戴维斯成为迈尔斯·D。
我使用以下 JS 实现了这一点:
$('.trunc-name').each(function (d, td) {
$(td).text($(td).text().replace(/^(\S+)\s+(\S).*/, '$1 $2.'));
});
td课程设置为trunc-name:
<td class="trunc-name"><?php foreach ($resident_name_terms as $object) { echo $object->name; } ?></td>
这些表内容(实际上是 WP 分类术语)可通过选择表单进行过滤 - 其选项也被截断了。
选择:
if( $terms = get_terms( array(
'taxonomy' => 'resident_name',
'orderby' => 'name'
) ) ) :
echo '<select name="resident_name_filter"><option value="" selected="selected">' . __( 'All Residents', 'mv' ) . '</option>';
foreach ( $terms as $term ) :
echo '<option class="trunc-name" value="' . $term->term_id . '">' . $term->name . '</option>';
endforeach;
echo '</select>';
endif;
到目前为止一切正常。问题是,当我通过该列(使用 AJAX 构建)的选择选项更改标准时,新输出的值不会被截断。
查询参数:
$relation = 'AND';
$params = array();
$args['tax_query']['relation'] = $relation;
foreach ( $taxonomies as $tax ) {
if( isset( $_POST[ $tax . '_filter' ] ) && !empty( $_POST[ $tax . '_filter' ] ) ) {
$args['tax_query'][] = array(
'taxonomy' => $tax,
'field' => 'id',
'terms' => $_POST[ $tax . '_filter' ],
);
}
};
环形:
$query = new WP_Query( $args );
if( $query->have_posts() ) : ?>
<table style="width:100%" id="incident-reports">
<tr>
<th>Resident</th>
</tr>
<?php
while( $query->have_posts() ): $query->the_post();
$resident_name_terms = get_the_terms( $post->ID, 'resident_name' );
?>
<tr>
<td class="trunc-name"><?php foreach ($resident_name_terms as $object) { echo $object->name; } ?></td>
</tr>
<?php endwhile; ?>
</table>
endif;
一如既往,非常感谢任何帮助!
慕桂英3389331