登录  /  注册
博主信息
博文 263
粉丝 3
评论 2
访问量 129348
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
wordpress在用户信息里增加从所有分类里面选取最喜欢的产品功能(自写)。。。待续
福哥的博客
原创
951人浏览过

存储最喜欢产品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 );
   
}

前台展示产品

待续


本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学