向快捷码添加选项

时间:2013-10-17 作者:NW Tech

我正在尝试编写一个短代码,允许该选项显示所有用户或显示单个用户,由用户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>\';
}
我对编写函数还是新手,所以如果我的语法不是最适合它使用的,请原谅我

2 个回复
最合适的回答,由SO网友:luistinygod 整理而成

关于编辑#3,我会这样重写它:

//* Shortcode for getting users
function list_of_users( $atts ) {

    extract( shortcode_atts(
        array(
            \'display\' => \'all\',
            \'user\' => \'30\'
        ),
        $atts
    ));

    $content = \'\';
    switch ( $display ) {
        case \'all\':
            $content = display_all_users();
            break;
        case \'single\':
            $content = display_single_user( (int) $user );
            break;
        default:
        break;
    }
    return $content;
}
add_shortcode(\'staff\', \'list_of_users\');

function display_all_users(){ 
    $args = array(
        \'orderby\' => \'ID\',
        \'order\' => \'ASC\'
    );
    $users = get_users( $args );

    $html = \'<ul class="staff">\';
        foreach( $users as $user ){ 
            $user_info = get_userdata($user->ID);
            $html .= \'<li>\';
            $html .= \'<a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-image">\';
            $html .= mt_profile_img( $user->ID, array(\'size\' => \'250x250\', \'echo\' => false));
            $html .= \'</a>\';
            $html .= \'<div class="staff-info"><a href="\'.get_home_url().\'/author/\'.$user_info->user_nicename.\'" class="staff-name"><h2>\'.$user->display_name.\'</h2></a>\';
            $html .= \'<div class="service-certs">\';
            $html .= get_field(\'certifications\',\'user_\'.$user->ID);
            $html .= \'</div>\';
            $html .= \'<p class="service-excerpt">\';
            $html .= get_field(\'short_bio\',\'user_\'.$user->ID);
            $html .= \'</p>\';
            $html .= \'<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>\';
            $html .= \'</li>\';
        }
    $html .= \'</ul>\';
    return $html;
}

function display_single_user( $user_id = 30 ){

    $html = \'<div class="home-profile-image">\';
    $html .=  mt_profile_img( $user_id, array(\'size\' => \'175x175\', \'echo\' => false));
    $html .= \'</div><div class="home-short-bio">\';
    $html .=  get_field(\'short_bio\',\'user_\'.$user_id);
    $html .= \'</div><a href="\'.get_home_url().\'/our-team/" class="button">Read more staff bios</a>\';

    return $html;
}

SO网友:mrwweb

您应该看看shortcode_atts() page 在法典中:

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        \'foo\' => \'no foo\',
        \'bar\' => \'default bar\',
    ), $atts, \'bartag\' ) );
    // etc...
这个很棒的小函数解析并设置具有两个潜在参数的短代码的默认值:

[bartag foo="" bar=""]
如果未提供任一属性,则foobar 参数设置为数组中提供的默认值。

因此,对于您的短代码,我可以想象这样的情况(未经测试的psuedo-ish代码):

function my_list_users( $atts ){
    // extract and set defaults
    extract( shorcode_atts( array(
        \'display\' => \'all\',
        \'user\' => 1
    ), $atts, \'list_users\' ) );

    // now vary your output based on $display att
    switch ( $display ) {

        case \'all\':
            $content = my_display_all_users();
            break;

        case \'single\':
            $content = my_display_single_user( (int) $user );
            break;

        default:
            // do something?
            break;
    }

    // give the shortcode something to output
    return $content;
}
add_shortcode( \'my_list_users\', \'my_list_users\' );
为了保持整洁,我可能会将列出所有用户和单个用户的函数拆分为单独的函数,并像上面那样从shortcode函数调用它们。此外,通常最好在函数前面加前缀(例如my_ 防止与其他短代码和函数发生任何冲突。

结束

相关推荐

private functions in plugins

我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。