WordPress纯代码文章实现折叠点击查看抽屉效果

2022-09-11 0 1,235

教程前言:

这个教程是汇站在别的地方上看到的,现在拿出来分享给大家。
刚开始觉得不需要这个功能。最近突然发现,对于一些频繁更新的文章,一下子太长的文章长度确实显得有点臃肿。在这个快速的信息时代,大部分人都不喜欢长篇大论,所以有时候需要把部分内容折叠起来。用户可以选择查看自己感兴趣的内容,然后结合本站的实践来实现这个功能。

添加代码

把下方代码添加到当前主题的 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">&nbsp;</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() {
?&gt;
    <script type="text/javascript">
      if ( typeof QTags != 'undefined' ) {
            QTags.addButton( 'collapse', '展开/收缩按钮', '[/collapse title="点击展开 查看更多"]','[/collapse]' );
        } 
    </script>
&lt;?php 
}
add_action('admin_print_footer_scripts', 'appthemes_add_collapse' );

本文章已结束,如转载请注明:汇站网 » WordPress 纯代码文章实现折叠点击查看抽屉效果

收藏 (0)

微信支付 微信扫一扫

支付宝支付 支付宝扫一扫

打赏二维码
点赞 (0)

站长资源下载中心-找源码上汇站

常见问题
  • 如果付款后没有弹出下载页面,多刷新几下,有问题联系客服!
查看详情
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
查看详情

相关文章

联系官方客服

为您解决烦忧 - 24小时在线 专业服务