Advertisement
arie_cristianD

custom co authors shortcode example

Apr 6th, 2025
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | None | 0 0
  1. function display_multiple_users_from_acf( $atts ) {
  2.     global $post;
  3.  
  4.     // Get the array of users from the ACF field
  5.     $users = get_field( 'co_author', $post->ID ); // Replace 'co_author' with your actual field name
  6.  
  7.     // Check if the data is valid
  8.     if ( ! $users || ! is_array( $users ) ) {
  9.         return '';
  10.     }
  11.  
  12.     $output = '<div class="acf-multi-user-list">';
  13.  
  14.     foreach ( $users as $user ) {
  15.         if ( ! is_object( $user ) ) {
  16.             continue;
  17.         }
  18.  
  19.         $display_name = esc_html( $user->display_name );
  20.         $email        = esc_html( $user->user_email );
  21.         $website      = esc_url( $user->user_url );
  22.         $avatar       = get_avatar( $user->ID, 64 ); // Avatar size: 64px
  23.  
  24.         $output .= '<div class="acf-user-box" style="margin-bottom:20px;">';
  25.         $output .= "<div class='acf-user-avatar'>{$avatar}</div>";
  26.         $output .= "<p><strong>Name:</strong> {$display_name}</p>";
  27.         $output .= "<p><strong>Email:</strong> {$email}</p>";
  28.         if ( $website ) {
  29.             $output .= "<p><strong>Website:</strong> <a href='{$website}' target='_blank'>{$website}</a></p>";
  30.         }
  31.         $output .= '</div>';
  32.     }
  33.  
  34.     $output .= '</div>';
  35.  
  36.     return $output;
  37. }
  38. add_shortcode( 'acf_users_display', 'display_multiple_users_from_acf' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement