教程前言:
这个教程是汇站在别的地方上看到的,现在拿出来分享给大家。
刚开始觉得不需要这个功能。最近突然发现,对于一些频繁更新的文章,一下子太长的文章长度确实显得有点臃肿。在这个快速的信息时代,大部分人都不喜欢长篇大论,所以有时候需要把部分内容折叠起来。用户可以选择查看自己感兴趣的内容,然后结合本站的实践来实现这个功能。
添加代码
把下方代码添加到当前主题的 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 class="xContent" style="display: none;">'.$content.'
';
}
add_shortcode('collapse', 'xcollapse');
添加后台快捷按钮
//添加展开/收缩快捷标签按钮
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' );
本文章已结束,如转载请注明:汇站网 » WordPress 纯代码文章实现折叠点击查看抽屉效果