为了子孙后代,以下是我最终要做的事情。
使用artist_sort_name
用于排序,只需将该字段中的值更改为artist_first_name
或artist_last_name
(感谢s\\u ha\\u dums的回答)。
我正在使用WPAlchemy 类生成元数据库和自定义字段。我不得不打电话给save_filter
在保存之前运行函数(这是functions.php
文件):
// Save filter function, check the value of alphabetize custom field
// if it\'s set then use first name as sort name
function nd_meta_sort($meta, $post_id) {
if ($meta[\'alphabetize\'] == "alphabetize") {
$meta[\'artist_sort_name\'] = $meta[\'artist_first_name\'];
} else {
$meta[\'artist_sort_name\'] = $meta[\'artist_last_name\'];
}
return $meta;
}
下面是在
functions.php
:
// Metabox for artists
$custom_metabox_artist = new WPAlchemy_MetaBox(array(
\'id\' => \'artist_custom_meta\',
\'title\' => \'Artist Info\',
\'template\' => TEMPLATEPATH . \'/assets/meta-artists.php\',
\'types\' => array(\'artists\'),
\'mode\' => WPALCHEMY_MODE_EXTRACT,
\'hide_screen_option\' => TRUE,
\'save_filter\' => \'nd_meta_sort\' // This get executed when saving the post
));
这是实际的代谢箱:
<table>
<tbody>
<tr>
<th>
<label>First Name:</label>
</th>
<td>
<!-- Field for the first name -->
<?php $mb->the_field(\'artist_first_name\'); ?>
<input name="<?php $mb->the_name(); ?>" class="artist-name" type="text" value="<?php $mb->the_value(); ?>">
<!-- Checkbox for for choosing which field to sort -->
<span>
<?php $mb->the_field(\'alphabetize\'); ?>
<input type="checkbox" name="<?php $mb->the_name(); ?>" value="alphabetize" <?php $mb->the_checkbox_state(\'alphabetize\'); ?>>
Alphabetize First Name
</span>
</td>
</tr>
<tr>
<th>
<label>Lastname:</label>
</th>
<td>
<!-- Field for the last name -->
<?php $mb->the_field(\'artist_last_name\'); ?>
<input name="<?php $mb->the_name(); ?>" class="artist-name" type="text" value="<?php $mb->the_value(); ?>">
</td>
</tr>
</tbody>
</table>
<!-- The field the post are sorted to. Note that it\'s hidden -->
<?php $mb->the_field(\'artist_sort_name\'); ?>
<input name="<?php $mb->the_name(); ?>" class="artist-name sort-name" type="hidden" value="<?php $mb->the_value(); ?>">
The
table
这并不是强制性的,只是标签/字段pare通常在WP后端显示的方式。
它看起来像这样:
为了完整起见,这是一个让艺术家们着迷的循环。我正在使用pre_get_posts
在里面functions.php
:
function artist_list($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
// Target the artists page
if (is_tax(\'artist-category\')) {
$query->set(\'posts_per_page\', \'-1\');
$query->set(\'orderby\', \'meta_value\');
$query->set(\'meta_key\', "artist_sort_name");
$query->set(\'order\', \'ASC\');
}
}
add_action(\'pre_get_posts\', \'artist_list\');