Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*** code à copier dans le functions.php du thème actif à partir d'ici ***/
- /**
- * on utilise ici un hook disponible dans le template archive.php de bp-default
- * en cas de thème différent, il suffit d'ajouter <?php do_action( 'bp_before_archive' );?>
- * dans le template category.php ou archive.php
- */
- add_action( 'bp_before_archive', 'chritchan_list_user_for_category');
- function chritchan_list_user_for_category() {
- if( !is_category() )
- return false;
- /*
- d'abord on ne sait jamais.. si un post était attaché à plusieurs catégories..
- autant utiliser un tableau pour stocker tous les ids de catégorie.
- */
- foreach( ( get_the_category() ) as $category) {
- $cat_args[] = $category->cat_ID;
- }
- if( !is_array( $cat_args ) && count( $cat_args ) < 1 )
- return false;
- $cats = implode(',', $cat_args );
- // on définit les arguments à passer au members loop
- $member_args = array( 'meta_key' => 'chritchan_dept', 'meta_value' => $cats );
- ?>
- <div id="chritchan-members">
- <?php if ( bp_has_members( $member_args ) ) : ?>
- <ul id="members-list" class="item-list" role="main">
- <?php while ( bp_members() ) : bp_the_member(); ?>
- <li>
- <div class="item-avatar">
- <a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
- </div>
- <div class="item">
- <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
- </div>
- <div class="clear"></div>
- </li>
- <?php endwhile; ?>
- </ul>
- <?php else: ?>
- <!-- on affiche rien ! -->
- <?php endif; ?>
- </div>
- <?php
- }
- /**
- * on modifie la requête de la fonction BP_Core_User::get_users
- *********************************************************************
- * Attention cette méthode sera dépréciée à partir de BuddyPress 1.7 *
- *********************************************************************
- * voir ce code pour un exemple des modifications à envisager à partir de la 1.7 :
- * https://github.com/imath/bp-mystery-man-hunt/blob/master/includes/cubepoints.php#L125
- */
- add_filter('bp_core_get_paged_users_sql', 'bpmmh_cubepoints_umeta_filter_select', 10, 2);
- function bpmmh_cubepoints_umeta_filter_select($query, $sql) {
- /* check the meta before modifying the query...*/
- if( !empty($sql['where_meta']) && strpos( $sql['where_meta'], 'chritchan_dept') && strpos( $sql['where_meta'], ',') ) {
- preg_match( '/umm.meta_value \= \'([0-9,]+)\'/', $sql['where_meta'], $meta_match );
- $sql['where_meta'] = str_replace( $meta_match[0], 'umm.meta_value IN ('. $meta_match[1] .')', $sql['where_meta']);
- $query = join( ' ', (array)$sql );
- }
- return $query;
- }
- /*** fin de l'ajout des membres dans template category ***/
- /*** pour mémoire l'ancien code ***/
- function chritchan_get_select_cat( $selected_id = false ) {
- $args = array(
- 'orderby' => 'name', /* ou id ou count .. */
- 'order' => 'ASC', /* ou DESC */
- 'hide_empty' => 0 /* ou 1 si on ne veut que des catégories ayant des articles rattachés */
- );
- // si custom post type utiliser l'identifiant de la custom taxo au lieu de category
- $cats = get_terms( 'category', $args );
- $select = '<select name="_dept_chritchan"><option value="0">Choisir</option>';
- foreach( $cats as $term ) {
- $selected = false;
- if( $selected_id == $term->term_id )
- $selected = 'selected';
- $select .= '<option value="'.$term->term_id.'" '.$selected.' >'.$term->name.'</option>';
- }
- $select .= '</select>';
- // si on veut modifier par un filtre le select box
- return apply_filters('chritchan_get_select_cat', $select, $cats );
- }
- function edit_chritchan_select_cat() {
- global $bp;
- $user_id = $bp->displayed_user->id;
- $selected_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
- if( bp_is_user_profile_edit() ) {
- ?>
- <label for="_dept_chritchan">Votre département</label>
- <?php echo chritchan_get_select_cat( $selected_id );?>
- <?php
- } else {
- $term_id = get_user_meta( $user_id, 'chritchan_dept' ,true );
- if( empty( $term_id ) )
- return false;
- // changer category pour l'identifiant de la custom taxo
- $cat = get_term_by( 'id', $term_id, 'category' );
- ?>
- <table class="profile-fields">
- <tr<?php bp_field_css_class(); ?>>
- <td class="label">Les articles de votre département</td>
- <td class="data"><a href="<?php echo get_term_link( $cat, 'category' );?>"><?php echo $cat->name?></a></td>
- </tr>
- </table>
- <?php
- }
- }
- add_action( 'bp_after_profile_field_content', 'edit_chritchan_select_cat');
- function save_chritchan_select_cat( $user_id, $posted_field_ids, $errors) {
- if( !empty( $_POST['_dept_chritchan'] ) )
- update_user_meta($user_id, 'chritchan_dept', intval( $_POST['_dept_chritchan'] ) );
- if( $_POST['_dept_chritchan'] == 0 )
- delete_user_meta( $user_id, 'chritchan_dept' );
- }
- add_action('xprofile_updated_profile', 'save_chritchan_select_cat', 10, 3);
- /*** fin de la copie ***/
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement