存储最喜欢产品ID:
function文件里添加以下代码:
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3>Favorite product</h3>
<?php
$args = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => 1525,
'orderby' => 'menu_order',
'order' => 'ASC',
'nopaging' => true,
);
$value = esc_attr( get_the_author_meta( 'favorite_product', $user->ID ) );
$value = explode("#",$value);
$child_pages = get_posts($args);
$count = count($child_pages);
if($count > 0){
foreach($child_pages as $post) {
setup_postdata($post);
$title = $post->post_title;
$id = $post->ID;
?>
<div class="acf-field acf-taxonomy-field" style="width: 33.33%; min-height: 262px; float: left; padding: 9px 12px 0 0;">
<div class="acf-label"><label><?php echo $title ?></label></div>
<div class="categorychecklist-holder">
<ul class="acf-checkbox-list acf-bl">
<?php
$args1 = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $id,
'orderby' => 'menu_order',
'order' => 'ASC',
'nopaging' => true,
);
$child_pages1 = get_posts($args1);
$count1 = count($child_pages1);
if($count1 > 0){
echo "<ul>";
$arr = array(1,2,3);
foreach($child_pages1 as $post1) {
setup_postdata($post1);
$title1 = $post1->post_title;
$id1 = $post1->ID;
?>
<li><label><input id="_zzurl[]" name="_zzurl[]" value='<?php echo $id1 ; ?>' <?php if(in_array($id1,$value)){echo "checked";};?> type="checkbox"> <span><?php echo $title1 ?></span></label></li>
<?php
}
} ?>
</ul>
</div>
</div>
<?php
}
} ?>
<?php }
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
$my_data=array();
for($z=0;$z<count($_POST['_zzurl']);$z++){
$my_data[$z] = $_POST['_zzurl'][$z];
}
$data = implode("#",$my_data);
$data = sanitize_text_field( $data );
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_usermeta( $user_id, 'favorite_product', $data );
}前台展示产品
待续
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号