wordpress知识库
网站首页 > 知识库 > wordpress知识 >

wordpress添加点击展开再点击收缩功能

2012/06/22

在撰写博客文章时,尤其是知识付费类文章,我们常常需要提供大量的补充资料。然而,如果将这些补充资料一一列出来,页面会变得冗长,给用户带来不好的阅读体验。因此,插件CC为我们提供了一种解决方案,即为WordPress主题添加”点击展开/收缩”的短代码功能。通过这个功能,我们可以首先隐藏某些部分的内容,让重点内容优先展示给用户,使网页在丰富内容的同时更加简洁清晰。这也提升了用户的浏览体验,完美的解决了这个问题!

步骤一、在functions.php中新增以下代码:

function addok($atts, $content = null){
extract(shortcode_atts(array("title"=>""),$atts));
return '<div style="margin: 0.5em 0;">
    <div class="cczhankai">
        <span class="biaoti">'.$title.'</span> 
        <a href="javascript:void(0)" class="CCbtn">展开/收缩</a>
        <div style="clear: both;"></div>
    </div>
    <div class="chajianOk" style="display: none;">'.$content.'</div>
</div>';
}
add_shortcode('collapse', 'addok');

步骤二、在页面中引用以下代码,通常为single.php:

jQuery(document).ready(
  function(jQuery){
    jQuery('.CCbtn').click(function(){
      jQuery(this).parent().parent().find('.chajianOk').slideToggle('slow');
    });
  }
});