我已在functions.php
子主题一些用户元和作品没有问题,但我只需要在一个名为“rivenditor”的特定用户角色的用户配置文件中显示这些数据,如果是,那么角色是“installatori”,我需要显示另一个表,我该怎么办?
以下是已尝试的代码
add_action( \'show_user_profile\', \'stel_extra_field_meta\' );
add_action( \'edit_user_profile\', \'stel_extra_field_meta\' );
function stel_extra_field_meta( $user ) {
?>
<h3>STEL Dati Rivenditore</h3>
<table class="form-table">
<tr>
<th><label for="location">Location</label></th>
<td><input type="text" name="location" value="<?php echo esc_attr(get_the_author_meta( \'location\', $user->ID )); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="regione">Regione</label></th>
<td><input type="text" name="regione" value="<?php echo esc_attr(get_the_author_meta( \'regione\', $user->ID )); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="tipo_rivenditore">Tipo di Rivenditore</label></th>
<td><input type="text" name="tipo_rivenditore" value="<?php echo esc_attr(get_the_author_meta( \'tipo_rivenditore\', $user->ID )); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="service_type">Servizio Scelto</label></th>
<?php $value = esc_attr(get_the_author_meta( \'service_type\', $user->ID )); $service_type = preg_replace(\'/\\|.*/\', \'\', $value); ?>
<td><input type="text" name="service_type" value="<?php echo $service_type; ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="phone">Phone</label></th>
<td><input type="text" name="phone" value="<?php echo esc_attr(get_the_author_meta( \'phone\', $user->ID )); ?>" class="regular-text" /></td>
</tr>
</table>
<?php
};
get_the_author_meta( $field, $userID );
update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
add_action( \'personal_options_update\', \'stel_save_extra_field_meta\' );
add_action( \'edit_user_profile_update\', \'stel_save_extra_field_meta\' );
function stel_save_extra_field_meta( $user_id )
{
update_user_meta( $user_id,\'location\', $_POST[\'location\'] );
update_user_meta( $user_id,\'service_type\', $_POST[\'service_type\'] );
update_user_meta( $user_id,\'phone\', $_POST[\'phone\'] );
update_user_meta( $user_id,\'regione\', $_POST[\'regione\'] );
update_user_meta( $user_id,\'tipo_rivenditore\', $_POST[\'tipo_rivenditore\'] );
}
SO网友:Christine Cooper
在您的author.php
文件,通过获取用户元数据get_user_meta(), 然后执行以下语句$user->roles == \'rivenditore\'
, 根据需要显示元数据,然后执行elseif $user->roles == \'installatori\'
语句,其中包含您希望为其他角色的用户显示的内容。
编辑:根据要求编辑代码示例:
// get author data
$queried_object = get_queried_object();
// set author ID
$author_id = $queried_object->ID;
// get author roles in array
$roles_arr = $queried_object->roles;
// get the meta data, I set it to \'location\' just to show you how it works
$user_meta_location = get_user_meta($author_id, \'location\', TRUE);
if ($roles_arr[0] == \'rivenditore\') {
// do stuff for rivenditore users
echo $user_meta_location;
} elseif ($roles_arr[0] == \'installatori\') {
// do other stuff for installatori users
echo $user_meta_location;
}
我还没有测试过,但应该可以。请注意,我不知道meta\\u数据的键是什么,所以请设置它。我添加了评论,所以所有内容都应该是可读的。祝你好运