博客列表 >wordpress在用户信息里增加从所有分类里面选取最喜欢的产品功能(自写)。。。待续

wordpress在用户信息里增加从所有分类里面选取最喜欢的产品功能(自写)。。。待续

福哥的博客
福哥的博客原创
2017年08月16日 18:50:32819浏览

存储最喜欢产品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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议