WP 美化

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

3年前 (2020-10-10)   1.71k   1178 字

WordPress 写文章时,有的内容会比较长,会影响页面美观等。更新日志也更适合点开/收缩按钮。

这时只能通过“展开/收缩”按钮将内容隐藏起来,这样更加简洁有条理。

以下是纯代码实现可以将文章部分内容隐藏,点击按钮可展开。再次点击按钮可以恢复隐藏。

效果预览
将以下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', '展开/收缩按钮', '
' ); } </script> <?php } add_action('admin_print_footer_scripts', 'appthemes_add_collapse' );
版权声明:Jakehu 发表于 2020-10-10 11:35:29
转载请注明:WordPress 文章页添加展开/收缩按钮︱Jakehu

您可能感兴趣的

2 条评论

  • 史蒂夫 ( VIP 1 )

    回复

    你好,展开收缩那个+号能不能变化一下啊

    • 回复

      可以的,自行替换ICON图标就可以的