将两个 wordpress 函数转换为一个

Convert two wordpress function to one

我正在使用以下函数:

function shortcode_title($params = array(), $content) {
extract(shortcode_atts(array(
'type' => ''
), $params));

return
'<div class="item js-item"><div class="header js-header">' .
($type ? " class=\"$type\"" : '') .
do_shortcode($content) .
'</div>';
}
add_shortcode('acc_title', 'shortcode_title');

function shortcode_content($params = array(), $content) {
extract(shortcode_atts(array(
'type' => ''
), $params));

return
'<div class="body js-body"><div class="body__contents">' .
($type ? " class=\"$type\"" : '') .
do_shortcode($content) .
'</div> </div></div>';
}
add_shortcode('acc_content', 'shortcode_content');

在 post 编辑中:

[acc_title] Custom Title [/acc_title]<br/>
[acc_content] Custom Content [acc_content]

所以结果是:

Custom Title
Custom content

如何更改函数以获取以下简码?

[acc title="Custom Title"] Custom Content [/acc]

最快的方法是添加 title 作为短代码属性,然后在返回之前连接标题和内容。

像这样:

add_shortcode('acc_title', 'shortcode_title');
function shortcode_title($params = array(), $content) {
    extract(
        shortcode_atts(
            array(
                'type' => '',
                'title'=> '', //added title to shortcode
            ), 
            $params
        )
    );
    //set "type" class
    $type = ($type ? " class=\"$type\"" : '');

    //concatenate outputs
    $output = '<div class="item js-item"><div class="header js-header'.$type.'">'.do_shortcode($content) .'</div></div>';
    $output .= '<div class="body js-body"><div class="body__contents'.$type.'">' .do_shortcode($content) .'</div></div>';

    return $output
}

这将允许您使用这样的简码:

[acc title="Custom Title" type="my-type"] Custom Content [/acc]