jQuery 手风琴和 Wordpress 简码

发布于 2024-11-18 07:26:29 字数 323 浏览 2 评论 0原文

我正在尝试让 jQuery 手风琴/选项卡简码看起来像这样

[accordions]
[accordion title="Accordion 1"]Accordion 1 Content[/accordion]
[accordion title="Accordion 2"]Accordion 2 Content[/accordion]
[accordion title="Accordion 3"]Accordion 3 Content[/accordion]
[/accordions]

我已经尝试了很多方法,但我似乎无法让它工作

这该怎么办?

I'm trying to get a jQuery accordion/tab Shortcode to look like this

[accordions]
[accordion title="Accordion 1"]Accordion 1 Content[/accordion]
[accordion title="Accordion 2"]Accordion 2 Content[/accordion]
[accordion title="Accordion 3"]Accordion 3 Content[/accordion]
[/accordions]

I've tried many ways but I just cant seem to get it to work

How can this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-11-25 07:26:29

我尚未测试此代码,但这应该可以工作:

function accordian_open_tag( $atts, $content='' ) {
  return '<div class="accordion">{$content}</div>';
}
function accordian_section( $atts, $content='' ) {
  $atts = shortcode_atts( array(
    'title' => 'default title'
  ), $atts );

  return "<h3><a href=\"#\">{$atts['title']}</a></h3>" . 
         "<div>{$content}</div>";
}
add_shortcode( 'accordians', 'accordian_open_tag' );
add_shortcode( 'accordian', 'accordian_section' );

加载页面时,您需要运行 $(".accordian").accordian() 才能正常工作。

I haven't tested this code, but this should work:

function accordian_open_tag( $atts, $content='' ) {
  return '<div class="accordion">{$content}</div>';
}
function accordian_section( $atts, $content='' ) {
  $atts = shortcode_atts( array(
    'title' => 'default title'
  ), $atts );

  return "<h3><a href=\"#\">{$atts['title']}</a></h3>" . 
         "<div>{$content}</div>";
}
add_shortcode( 'accordians', 'accordian_open_tag' );
add_shortcode( 'accordian', 'accordian_section' );

You will need to run $(".accordian").accordian() when the page is loaded for this to work.

醉南桥 2024-11-25 07:26:29
<?php
// my shortcode like 
/*
[accordion] 
    [section title="Your title #1" class="active" ] 
    Your Description here #1
    [/section]
    [section title="Your title #2"]
    Your Description here #2
    [/section]
    [section title="Your title #3"]
    Your Description here #3
    [/section]
[/accordion]  
*/
add_shortcode( 'section', 'section' );
function section( $atts, $content ){
     extract(shortcode_atts(array(
        'title' => 'Collapse',
        'id'    => false,
        'class' => false,
    ), $atts));
    $GLOBALS['section'][] = array( 
        'title'   =>  $title ,
        'id'      =>  $id,
        'class'   =>  $class ,
        'content' =>  $content ,
    );
    $id  = "collapse-id-".$GLOBALS['collapsibles_count'];
    foreach( $GLOBALS['section'] as $tab ){         
        $class = ( !empty($tab['class']) && $tab['class']=="active" ) ? "panel-collapse collapse in"  : "panel-collapse collapse";      
        $__title = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($tab['title'])  );     
        $return = sprintf( "\n".'<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#%s" href="#%s">%s</a></h4></div><div id="%s" class="%s"><div class="panel-body">%s</div></div></div>'."\n",$id, $__title, $tab['title'], $__title, $class, $tab['content']);       
    } // foreach        
    return do_shortcode($return);
}// function ending
add_shortcode( 'accordion', 'accordion' );
function accordion( $atts, $content ){ 
    if(isset( $GLOBALS['collapsibles_count'] )) {
        $GLOBALS['collapsibles_count']++;
    }else {
        $GLOBALS['collapsibles_count'] = 0; 
    }
    $id  = "collapse-id-".$GLOBALS['collapsibles_count'];
    return  do_shortcode(sprintf('<div class="tp-accordion"><div class="panel-group" id="%s"> %s </div></div> ', $id, $content));
}
?>

<?php
// my shortcode like 
/*
[accordion] 
    [section title="Your title #1" class="active" ] 
    Your Description here #1
    [/section]
    [section title="Your title #2"]
    Your Description here #2
    [/section]
    [section title="Your title #3"]
    Your Description here #3
    [/section]
[/accordion]  
*/
add_shortcode( 'section', 'section' );
function section( $atts, $content ){
     extract(shortcode_atts(array(
        'title' => 'Collapse',
        'id'    => false,
        'class' => false,
    ), $atts));
    $GLOBALS['section'][] = array( 
        'title'   =>  $title ,
        'id'      =>  $id,
        'class'   =>  $class ,
        'content' =>  $content ,
    );
    $id  = "collapse-id-".$GLOBALS['collapsibles_count'];
    foreach( $GLOBALS['section'] as $tab ){         
        $class = ( !empty($tab['class']) && $tab['class']=="active" ) ? "panel-collapse collapse in"  : "panel-collapse collapse";      
        $__title = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($tab['title'])  );     
        $return = sprintf( "\n".'<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#%s" href="#%s">%s</a></h4></div><div id="%s" class="%s"><div class="panel-body">%s</div></div></div>'."\n",$id, $__title, $tab['title'], $__title, $class, $tab['content']);       
    } // foreach        
    return do_shortcode($return);
}// function ending
add_shortcode( 'accordion', 'accordion' );
function accordion( $atts, $content ){ 
    if(isset( $GLOBALS['collapsibles_count'] )) {
        $GLOBALS['collapsibles_count']++;
    }else {
        $GLOBALS['collapsibles_count'] = 0; 
    }
    $id  = "collapse-id-".$GLOBALS['collapsibles_count'];
    return  do_shortcode(sprintf('<div class="tp-accordion"><div class="panel-group" id="%s"> %s </div></div> ', $id, $content));
}
?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文