我正在尝试编写一个短代码,允许该选项显示所有用户或显示单个用户,由用户id或“nicename”定义。
短代码如下所示:[staff display="all"]
或[staff display="single" user="3"]
我有一个显示所有用户的标准快捷码,但我想将其用于上面示例中的选项。我不确定将其整合在一起的最佳途径。
下面是我现在使用的shortcode函数,它适用于[staff]
:
function list_users(){
$args = array(
\'orderby\' => \'ID\',
\'order\' => \'ASC\'
);
$users = get_users($args);
echo \'<ul class="staff">\';
foreach( $users as $user ){
$user_info = get_userdata($user->ID);
echo \'<li>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-image">\';
echo mt_profile_img( $user->ID, array(\'size\' => \'Services\',));
echo \'</a>\';
echo \'<div class="staff-info"><a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-name"><h2>\'.$user->display_name.\'</h2></a>\';
echo \'<div class="service-certs">\';
echo the_field(\'certifications\',\'user_\'.$user->ID);
echo \'</div>\';
echo \'<p class="service-excerpt">\';
echo the_field(\'short_bio\',\'user_\'.$user->ID);
echo \'</p>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="more-staff-bio">Read more from \' . $user_info->user_firstname . \'</a></div><div style="clear: both;"></div>\';
echo \'</li>\';
}
echo \'</ul>\';
}
add_shortcode(\'staff\', \'list_users\');
EDIT 根据下面答案的建议,以下是我的新功能
//* Shortcode for getting users
function display_all_users(){
$args = array(
\'orderby\' => \'ID\',
\'order\' => \'ASC\'
);
$users = get_users($args);
echo \'<ul class="staff">\';
foreach( $users as $user ){
$user_info = get_userdata($user->ID);
echo \'<li>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-image">\';
echo mt_profile_img( $user->ID, array(\'size\' => \'Services\',));
echo \'</a>\';
echo \'<div class="staff-info"><a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-name"><h2>\'.$user->display_name.\'</h2></a>\';
echo \'<div class="service-certs">\';
echo the_field(\'certifications\',\'user_\'.$user->ID);
echo \'</div>\';
echo \'<p class="service-excerpt">\';
echo the_field(\'short_bio\',\'user_\'.$user->ID);
echo \'</p>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="more-staff-bio">Read more from \' . $user_info->user_firstname . \'</a></div><div style="clear: both;"></div>\';
echo \'</li>\';
}
echo \'</ul>\';
}
function list_of_users( $atts ){
// extract and set defaults
extract( shorcode_atts( array(
\'display\' => \'all\',
\'user\' => 30
), $atts, \'staff\' ) );
// now vary your output based on $display att
switch ( $display ) {
case \'all\':
$content = display_all_users();
break;
case \'single\':
$content = display_single_user( (int) $user );
break;
default:
break;
}
// give the shortcode something to output
return $content;
}
add_shortcode(\'staff\', \'list_of_users\');
EDIT 2 我成功了,但是
single
函数将内容转储到其应该位于的div包装之外。
//* Shortcode for getting users
function list_of_users( $atts = array(), $content = null ){
extract(shortcode_atts(
array(
\'display\' => \'all\',
\'user\' => \'30\'
),
$atts
));
switch ( $display ) {
case \'all\':
$content = display_all_users();
break;
case \'single\':
$content = display_single_user( (int) $user );
break;
default:
break;
}
return $all;
}
add_shortcode(\'staff\', \'list_of_users\');
function display_all_users(){
$args = array(
\'orderby\' => \'ID\',
\'order\' => \'ASC\'
);
$users = get_users($args);
echo \'<ul class="staff">\';
foreach( $users as $user ){
$user_info = get_userdata($user->ID);
echo \'<li>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-image">\';
echo mt_profile_img( $user->ID, array(\'size\' => \'Services\',));
echo \'</a>\';
echo \'<div class="staff-info"><a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-name"><h2>\'.$user->display_name.\'</h2></a>\';
echo \'<div class="service-certs">\';
echo the_field(\'certifications\',\'user_\'.$user->ID);
echo \'</div>\';
echo \'<p class="service-excerpt">\';
echo the_field(\'short_bio\',\'user_\'.$user->ID);
echo \'</p>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="more-staff-bio">Read more from \' . $user_info->user_firstname . \'</a></div><div style="clear: both;"></div>\';
echo \'</li>\';
}
echo \'</ul>\';
}
function display_single_user(){
extract(shortcode_atts(
array(
\'display\' => \'all\',
\'user\' => \'30\'
),
$atts
));
$output = mt_profile_img( $user, array(\'size\' => \'Services\',)).the_field(\'short_bio\',\'user_\'.$user);
}
EDIT #3 我让输出工作并“缓冲到正确的区域。但我注意到变量
user
在短代码中使用的不会传递给
single
作用
//* Shortcode for getting users
function list_of_users( $atts = array(), $content = null ){
extract(shortcode_atts(
array(
\'display\' => \'all\',
\'user\' => \'30\'
),
$atts
));
ob_start();
switch ( $display ) {
case \'all\':
$content = display_all_users();
break;
case \'single\':
$content = display_single_user( (int) $user );
break;
default:
break;
}
$output = ob_get_clean();
return $all.$output;
}
add_shortcode(\'staff\', \'list_of_users\');
function display_all_users(){
$args = array(
\'orderby\' => \'ID\',
\'order\' => \'ASC\'
);
$users = get_users($args);
echo \'<ul class="staff">\';
foreach( $users as $user ){
$user_info = get_userdata($user->ID);
echo \'<li>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-image">\';
echo mt_profile_img( $user->ID, array(\'size\' => \'250x250\',));
echo \'</a>\';
echo \'<div class="staff-info"><a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-name"><h2>\'.$user->display_name.\'</h2></a>\';
echo \'<div class="service-certs">\';
echo the_field(\'certifications\',\'user_\'.$user->ID);
echo \'</div>\';
echo \'<p class="service-excerpt">\';
echo the_field(\'short_bio\',\'user_\'.$user->ID);
echo \'</p>\';
echo \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="more-staff-bio">Read more from \' . $user_info->user_firstname . \'</a></div><div style="clear: both;"></div>\';
echo \'</li>\';
}
echo \'</ul>\';
}
function display_single_user($args){
$args = extract(shortcode_atts(
array(
\'display\' => \'all\',
\'user\' => \'30\'
),
$atts, \'staff\'
));
echo\'<div class="home-profile-image">\';
echo mt_profile_img( $user, array(\'size\' => \'175x175\',));
echo \'</div><div class="home-short-bio">\';
echo the_field(\'short_bio\',\'user_\'.$user);
echo\'</div><a href="\'.get_home_url().\'/our-team/" class="button">Read more staff bios</a>\';
}
我对编写函数还是新手,所以如果我的语法不是最适合它使用的,请原谅我