admin管理员组文章数量:1297131
I've created a plugin that with a shortcode displays all users with the custom role "ansatt". This works perfectly fine, but I'd like to be able to set the role as a parameter within the shortcode.
This is the what I want to achieve: [hw_display_users role="ansatt"]
Below is the function I've created. bottom line is that I'm trying to move the 'role' => 'ansatt'
into the actual shortcode so that the role can easily be altered.
How on earth do I accomplish this?
function display_user_roles(){
$args = array(
'role' => 'ansatt',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
Edit - working code
function display_user_roles( $atts ){
$args = array(
'orderby' => 'user_nicename',
'order' => 'ASC'
);
if ( !empty( $atts['role'] ) )
$args['role'] = $atts['role'];
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
I've created a plugin that with a shortcode displays all users with the custom role "ansatt". This works perfectly fine, but I'd like to be able to set the role as a parameter within the shortcode.
This is the what I want to achieve: [hw_display_users role="ansatt"]
Below is the function I've created. bottom line is that I'm trying to move the 'role' => 'ansatt'
into the actual shortcode so that the role can easily be altered.
How on earth do I accomplish this?
function display_user_roles(){
$args = array(
'role' => 'ansatt',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
Edit - working code
function display_user_roles( $atts ){
$args = array(
'orderby' => 'user_nicename',
'order' => 'ASC'
);
if ( !empty( $atts['role'] ) )
$args['role'] = $atts['role'];
$users = get_users( $args );
ob_start();
echo '<div class="owl-carousel owl-theme">';
foreach ( $users as $user ) {
echo '<div class="item"><a href="' . get_author_posts_url( $user->ID, $user->user_nicename ) . '">';
$image1 = get_field('profilbilde', $user);
echo '<div class="hw-thumb">';
echo '<img class="profile1" src="';
echo $image1['url'];
echo '"/>';
echo '</div>';
echo '<div class="hw-stilling">'. get_field('stilling', $user) . '</div>';
echo '<h2 class="hw-username">'. esc_html( $user->display_name ) . '</h2>';
echo '<div class="hw-epostadresse">'. get_field('epostadresse', $user) . '</div>';
echo '</a></div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'hw_display_users', 'display_user_roles' );
Share
Improve this question
edited Apr 12, 2021 at 11:00
André Giæver
asked Apr 8, 2021 at 9:02
André GiæverAndré Giæver
32 bronze badges
1
- Are you asking how shortcode attributes work? – Tom J Nowell ♦ Commented Apr 8, 2021 at 9:38
1 Answer
Reset to default 1First, you'll have to get shortcode parameter(s). Change function's declaration to:
function display_user_roles( $atts ) {
Your shortcode will be either [hw_display_users]
or [hw_display_users role="roletopass"]
. The first (no parameter) will use role specified in $args array. Second will replace $args->role with parameter passed. Add
if ( !empty( $atts['role'] ) )
$args['role'] = $atts['role'];
before
$users = get_users( $args );
Remaining code should be ok.
For more information how to handle shortcodes with parameters read this.
本文标签: pluginsCustom shortcode for displaying user based on a role parameter
版权声明:本文标题:plugins - Custom shortcode for displaying user based on a role parameter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741621517a2388836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论