最近突然发觉对于有些经常更新的文章来说,一下子太长的篇幅的确显得有些臃肿,在这个快速的信息时代来说,大多数人不喜欢太长篇大论的文章,所以说有时候折叠一部分内容显得还是很有必要的,用户可以选择查看自己感兴趣的内容,那么接下来结合本站实践来实现这一功能。
效果展示
[collapse title=”点击展开 查看更多”]这个功能能让我们的文章内容看起来更加简洁,让访客自己选择查看。[/collapse]
功能教程
把下方代码添加到当前主题的footer.php文件中。
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script>
/* 为wordpress主题添加“内容展开/收缩”功能开始 */
jQuery(document).ready(
function(jQuery){
jQuery('.collapseButton').click(function(){
jQuery(this).parent().parent().find('.xContent').slideToggle('slow');
});
});
/* 为wordpress主题添加“内容展开/收缩”功能开始 */
</script>
将下方代码添加至主题目录下的functions.php中
// 文章页添加展开收缩效果
function xcollapse($atts, $content = null){
extract(shortcode_atts(array("title"=>""),$atts));
return '
<style>.xControl {
font-size: 15px;
font-weight: bold;
padding: 5px 0;
box-shadow:0 0 20px #d0d0d0;/* 阴影 */
background-color: #FFFFFF;/* 背景颜色 */
border-bottom: 2px solid #e74c3c;/* 边 */
transition: all 0.1s linear;
text-align: center;
border-radius: 0 0 5% 5%;
border-radius:4px;
}
.xControl a{
text-decoration: none;
display: block;}
.xControl a:hover {
text-decoration: none;
display: block;
color:red;
}
.xControl i{font-style:normal;}
</style>
<div style="margin: 0.5em 0;">
<div class="xControl">
<a href="javascript:void(0)" class="collapseButton xButton"> <i class="fa fa-toggle-on" aria-hidden="true"> </i><span class="xTitle">'.$title.'</span></a>
<div style="clear: both;"></div>
</div>
<div class="xContent" style="display: none;">'.$content.'</div>
</div>';
}
add_shortcode('collapse', 'xcollapse');
添加后台快捷按钮
把下面代码添加到主题目录下的functions.php,去掉第6行【collapse title=”点击展开 查看更多】前的斜杠/
//添加展开/收缩快捷标签按钮
function appthemes_add_collapse() {
?>
<script type="text/javascript">
if ( typeof QTags != 'undefined' ) {
QTags.addButton( 'collapse', '展开/收缩按钮', '[/collapse title="点击展开 查看更多"]','[/collapse]' );
}
</script>
<?php
}
add_action('admin_print_footer_scripts', 'appthemes_add_collapse' );
使用方法
在文章中插入以下代码
【collapse title="点击展开 查看更多"】收缩内容【/collapse】
© 版权声明
THE END
喜欢就支持一下吧