用WordPress编辑文章时,一些FAQ类的内容多且长,顺序写下来导致文章的排版过长,影响美观,还有些涉及代码的内容,如果让代码格式化显示的话也会让文章内容看起来很多,不方便访问者浏览。
可以通过简单的短代码实现文章或者页面内容折叠式展开/收缩,方法如下:
1.在header.php中添加下面的代码,或者也可以单独写进一个js文件中然后在header.php中引入也可以,我是直接在header.php中添加代码。
<script type=
"text/javascript"
>
jQuery(document).ready(
function
(jQuery) {
jQuery(
'.collapseButton'
).click(
function
() {
jQuery(this).parent().parent().find(
'.xContent'
).slideToggle(
'slow'
);
});
});
</script>
2.在function.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>
<a href=
"javascript:void(0)"
class
=
"collapseButton xButton"
>展开/收缩</a>
<div style=
"clear: both;"
></div>
</div>
<div
class
=
"xContent"
style=
"display: none;"
>
'.$content.'
</div>
</div>';
}
add_shortcode(
'collapse'
,
'xcollapse'
);
展开/收缩这几个字可以用图片代替。
3.可以优化一下代码,因为默认是靠左的,不好看,我们让他往中间一点显示,具体的距离可以自行调整。当然这一步忽略也是可以的。
在style.css中添加以下代码:
/*展开收缩样式*/
.xControl{height:56px; line-height:56px; border-bottom:1px solid #ececec; font-size:16px; font-weight:100; width:60%; margin:0px auto; padding-left:40px; color:#333333;}
.xControl a{font-size:17px; font-weight:bold; color:#333333;}
.xControl a:visited{font-size:17px; font-weight:bold; color:#333333;}
.xControl a:link{font-size:17px; font-weight:bold; color:#333333;}
.xContent{width:60%; margin:0px auto; line-height:25px;padding-left:40px;}}
具体样式可以根据网站的风格调整。
来使用此功能了。其中title是指添加一些提示内容,当然也可以省略title不写。
注意:
- 如果主题没有加载jQuery库,一定要先加载,不然不会生效。
- 无法实现短代码套嵌
来源:文章部分内容来自网络,由智能家居指南网整理发布,侵删!
发表评论