我想在发布群组或活动帖子的用户名旁边添加标签 标签将是管理员或版主 如果是管理员帖子,它将显示管理员标签,或者如果是版主帖子,它将显示版主标签 我成功添加了这些标签,但它从活动标题提要中消失了组标题。 这是我的c[在这里输入图片描述]()
function custom_activity_action_string( $action, $activity ) {
// Get the author role
$author_role = bbp_get_user_display_role($activity->user_id);
// Set the label based on user role
if ( $author_role == 'Keymaster' ) {
$label = '<span class="group-admin-label">Admin</span>';
} elseif ( $author_role == 'Participant' ) {
$label = '<span class="group-moderator-label">Moderator</span>';
} else {
$label = '';
}
// Check if activity is in group activity page or main activity page
if ( $activity->component == 'groups' && $activity->type == 'activity_update' ) {
$group_label = __( 'posted an update in the group', 'text-domain' );
} else {
$group_label = __( 'posted an update', 'text-domain' );
}
// Get user data
$user_link = bp_core_get_userlink( $activity->user_id );
$user_name = bp_core_get_user_displayname( $activity->user_id );
// Get the activity ID
$activity_id = bp_get_activity_id();
// Get the group ID
$group_id = bp_activity_get_meta( $activity_id, 'item_id', true );
// Get the group title
$group_title = bp_get_group_name( $group_id );
// Get group data if activity is in group activity page
if ( $activity->component == 'groups' ) {
$group_link = bp_get_group_permalink( $activity->item_id );
$group_avatar = bp_get_group_avatar_url( $activity->item_id, array( 'type' => 'full' ) ); // Replace this line with bp_get_group_logo_url to show the group logo
$group_name = bp_get_group_name( $activity->item_id );
$group_post_title = bp_activity_get_meta( $activity->id, 'bp_activity_title', true ); // Get the title of the post within the group
// Set label based on group type
$group_type = bp_get_group_type( $activity->item_id );
if ( $group_type == 'hidden' ) {
$group_label = __( 'posted an update in the private group', 'text-domain' );
}
}
// Build activity action string
$action = sprintf(
__( '%1$s %2$s %3$s %4$s %5$s %6$s', 'text-domain' ),
$user_link,
$label,
$group_label,
$group_title,
isset( $group_avatar ) ? '<img src="' . $group_avatar . '" alt="' . $group_name . '"/>' : '',
isset( $group_post_title ) ? $group_post_title : ''
);
// Add group name and link if activity is in group activity page
if ( $activity->component == 'groups' ) {
$action .= sprintf(
__( ' %1$s', 'text-domain' ),
'<a class ="grp-ttl" href="' . $group_link . '">group name' . $group_name . $group_title . '</a>',
);
}
return $action;
}
add_filter( 'bp_get_activity_action_pre_meta', 'custom_activity_action_string', 10, 2 );
这里是代码完整代码
function custom_activity_action_string( $action, $activity ) {
// Get the author role
$author_role = bbp_get_user_display_role($activity->user_id);
$author_id = bp_get_activity_user_id();
// Get the group ID
$group_id = bp_get_activity_item_id();
// Get the User ID
$user_id = get_current_user_id();
// Set the label based on user role
if ( groups_is_user_admin( $author_id, $group_id ) ) {
$label = '<span class="group-admin-label">Admin</span>';
} elseif ( groups_is_user_mod( $author_id, $group_id ) ) {
$label = '<span class="group-moderator-label">Moderator</span>';
} elseif ( groups_is_user_member( $author_id, $group_id ) ) {
$label = '';
} else {
$label = '';
}
// for show the user role for a group
// if ($author_role){
// $label = '<span class="author-role">' . $author_role . '</span>';
// }
// Check if activity is in group activity page or main activity page
if ( $activity->component == 'groups' && $activity->type == 'activity_update' ) {
$group_label = __( 'posted an update in the group', 'text-domain' );
} else {
$group_label = __( 'posted an update', 'text-domain' );
}
// Get user data
$user_link = bp_core_get_userlink( $activity->user_id );
$user_name = bp_core_get_user_displayname( $activity->user_id );
// Get the group ID
$group_id = bp_get_activity_item_id();
// Get the group object
$group = groups_get_group( array( 'group_id' => $group_id ) );
// Get the group title and link
$group_title = $group->name;
$group_link = bp_get_group_permalink( $group );
// Set label based on group type
$group_type = bp_get_group_type( $activity->item_id );
if ( $group_type == 'hidden' ) {
$group_label = __( 'posted an update in the private group', 'text-domain' );
}
// Build activity action string
$action = sprintf(
__( '%1$s %2$s %3$s', 'text-domain' ),
$user_link,
$label,
$group_label,
);
// Add group name and link if activity is in group activity page
if ( $activity->component == 'groups' ) {
$action .= sprintf(
__( ' <a class ="grp-ttl" href="%1$s">%2$s</a>', 'text-domain' ),
$group_link,
$group_title
);
}
return $action;
}
add_filter( 'bp_get_activity_action_pre_meta', 'custom_activity_action_string', 10, 2 );