可能......我创造了两个没有foreach的功能。并且,如果我不使用变量,它的工作原理:
add_action( 'show_user_profile', 'pglr_user_pages' );
add_action( 'edit_user_profile', 'pglr_user_pages' );
add_action( 'user_register', 'pglr_user_pages' );
function pglr_user_pages( $user ) {
$post_type = "page"; ?>
Asignar <?php echo $post_type;?>:
$selected = get_the_author_meta( 'user_custom_page', $user->ID );
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
echo '';
?>
>All <?php echo $label;?>
foreach ($posts as $post) {
echo 'ID ? ' selected="selected"' : '', '>', $post->post_title, '';
}?>
echo '';
?>
}
add_action( 'personal_options_update', 'save_pglr_user_pages' );
add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );
function save_pglr_user_pages( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//save dropdown
update_usermeta( $user_id, 'user_custom_page', $_POST['user_custom_page_value'] );
}
但是,此代码不起作用:
add_action( 'show_user_profile', 'pglr_user_pages' );
add_action( 'edit_user_profile', 'pglr_user_pages' );
add_action( 'user_register', 'pglr_user_pages' );
function pglr_user_pages( $user ) {
$post_type = "page"; ?>
Asignar <?php echo $post_type;?>:
**global $user_custom_post;**
**$user_custom_post = 'user_custom_' . $post_type;**
$selected = get_the_author_meta( **$user_custom_post**, $user->ID );
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
echo '';
?>
>All <?php echo $label;?>
foreach ($posts as $post) {
echo 'ID ? ' selected="selected"' : '', '>', $post->post_title, '';
}?>
echo '';
?>
}
add_action( 'personal_options_update', 'save_pglr_user_pages' );
add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );
function save_pglr_user_pages( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//save dropdown
update_usermeta( $user_id, **$user_custom_post,** $_POST['user_custom_page_value'] );
}
问题可能是使用变量......