欢迎来到站长天空!

WordPress

当前位置: 主页 > CMS教程 > WordPress

实现WordPress文章展开/收缩的效果

时间:2025-10-23 09:45:14|栏目:WordPress|点击:

1、修改footer.php文件

加入到body之前

<script>jQuery(document).ready(
function(jQuery){
jQuery('.collapseButton').click(function(){
jQuery(this).parent().parent().find('.xContent').slideToggle('slow');
});
});</script>

2、修改functions.php文件

加入到主题functions.php文件中

//展开收缩功能
function xcollapse($atts, $content = null){
 extract(shortcode_atts(array("title"=>""),$atts));
 return '<div style="margin: 0.5em 0;">
 <div class="xControl">
 <span class="xTitle">'.$title.'</span>&nbsp;==>&nbsp;<a href="JavaScript:void(0)" class="collapseButton xButton"><span class="xbtn02">展开/收缩</span></a>
 <div style="clear: both;"></div>
 </div>
 <div class="xContent" style="display: none;">'.$content.'</div>
 </div>';
}
add_shortcode('collapse', 'xcollapse');

3、代码使用:

{collapse title="标题"}需点击展开的内容{/collapse}

将上述代码中的{}分别替换为[]

补刀:CSS美化一下吧

/* 展开收缩*/.xControl {
 background: #f6f6f6;
}
.xTitle {
 color: #333;
}
.xbtn02{
 color: #428bca;
 text-decoration: underline;
}

在编辑文章的时候快速添加该按钮 :

//添加展开/收缩快捷标签按钮
 function appthemes_add_collapse() {
 ?><script type="text/javascript">// <![CDATA[
 QTags.addButton( 'collapse', '展开/收缩按钮', '{collapse title="说明文字"}','{/collapse}' );// ]]>
 </script><?php
 }
 add_action('admin_print_footer_scripts', 'appthemes_add_collapse' );

将上述代码中的{}分别替换为[]

将上述代码添加到主题functions.php文件中,然后在编辑文章的时候切换到文本模式,就可以看到该按钮.

按钮使用方法:先单击一次,然后输入你想要收缩的内容,再单击一次按钮,然后替换替换其中的说明文字。


上一篇:实现WordPress文章输入密码可见隐藏内容

栏    目:WordPress

下一篇:WordPress删除文章多余的自定义字段及值

本文标题:实现WordPress文章展开/收缩的效果

本文地址:https://121.40.58.76/CMSjiaocheng/25332.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 站长天空 版权所有 Powered by EyouCms冀ICP备14023439号