WordPress 文章页添加展开/收缩按钮

自己折腾之后,适合《Sirius》主题

https://www.jakehu.cn/691.html

由于之前的文章对另一个主题不起作用,又找了个……这个意外的可以使用(怀疑是我加入代码的方式错了,怀疑人生)

将以下JS代码添加到你的主js文件中

/* 文章页展开/收缩按钮JS效果*/
jQuery(document).ready(
function(jQuery){
jQuery('.collapseButton').click(function(){
jQuery(this).parent().parent().find('.xContent').slideToggle('slow');
});
});

在主题 functions.php 文件下添加以下代码

//文章页展开/收缩按钮
function xcollapse($atts, $content = null) {
extract(shortcode_atts(array( "title" => "" ) , $atts));
return '<div style="margin: 0.5em 0;"><div class="xControl"><a href="javascript:void(0)" class="collapseButton xButton"><i class="fa fa-plus-square" ></i> ' . $title . '</a><div style="clear: both;"></div></div><div class="xContent" style="display: none;">' . $content . '</div></div>';
}
add_shortcode('collapses', 'xcollapse');

短代码

在需要使用的地方手动输入 [collapses title=标题]内容[/collapses]

添加短代码到 functions.php

可以添加“展开/收缩按钮”,以后编辑文章时,鼠标直接点击按钮可直接插入短代码实现“展开/收缩”功能,不用再手动输入。

//文章页展开/收缩按钮
function appthemes_add_collapse() {
?>
<script type="text/javascript">
if ( typeof QTags != 'undefined' ) {
QTags.addButton( 'collapses', '展开/收缩按钮', '
<div style="margin: 0.5em 0;">
    <div class="xControl">
        <a href="javascript:void(0)" class="collapseButton xButton" style="color: #fff;display: block;"><i class="fa fa-plus-square"></i>  标题</a>
        <div style="clear: both;"></div>
    </div>
    <div class="xContent" style="display: none;">','</div>
</div>' );
}
</script>
<?php
}
add_action('admin_print_footer_scripts', 'appthemes_add_collapse' );