WordPress教程

WordPress首页文章页或其它自定义侧边栏

阿里云

一般主题都有带侧边栏的吧,大部分都在首页或文章页有设定,如果想在页面上关连侧边栏并且是独立于首页和文章页的呢,这时候就要添加这些代码了,在主题的 function.php 中添加:

  1. function widgetSetup(){
  2.     $args = array(
  3.         'name'          => '首页固定侧边栏',
  4.         'id'            => 'sidebar-index-affix',
  5.         'description'   => '显示在首页固定侧边栏小工具',
  6.         'class'         => 'custom',
  7.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  8.         'after_widget'  => '</div>',
  9.         'before_title'  => '<div class="widget-title">',
  10.         'after_title'   => '</div>'
  11.     );
  12.     register_sidebar($args);
  13.     $args = array(
  14.         'name'          => '首页侧边栏',
  15.         'id'            => 'sidebar-index',
  16.         'description'   => '显示在首页侧边栏小工具',
  17.         'class'         => 'custom',
  18.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  19.         'after_widget'  => '</div>',
  20.         'before_title'  => '<div class="widget-title">',
  21.         'after_title'   => '</div>'
  22.     );
  23.     register_sidebar($args);
  24.     $args = array(
  25.         'name'          => '文章页固定侧边栏',
  26.         'id'            => 'sidebar-article-affix',
  27.         'description'   => '显示在文章页固定侧边栏小工具',
  28.         'class'         => 'custom',
  29.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  30.         'after_widget'  => '</div>',
  31.         'before_title'  => '<div class="widget-title">',
  32.         'after_title'   => '</div>'
  33.     );
  34.     register_sidebar($args);
  35.     $args = array(
  36.         'name'          => '文章页侧边栏',
  37.         'id'            => 'sidebar-article',
  38.         'description'   => '显示在文章页侧边栏小工具',
  39.         'class'         => 'custom',
  40.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  41.         'after_widget'  => '</div>',
  42.         'before_title'  => '<div class="widget-title">',
  43.         'after_title'   => '</div>'
  44.     );
  45.     register_sidebar($args);
  46.     $args = array(
  47.         'name'          => '页面页固定侧边栏',
  48.         'id'            => 'sidebar-page-affix',
  49.         'description'   => '显示在页面页固定侧边栏小工具',
  50.         'class'         => 'custom',
  51.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  52.         'after_widget'  => '</div>',
  53.         'before_title'  => '<div class="widget-title">',
  54.         'after_title'   => '</div>'
  55.     );
  56.     register_sidebar($args);
  57.     $args = array(
  58.         'name'          => '页面页侧边栏',
  59.         'id'            => 'sidebar-page',
  60.         'description'   => '显示在页面页侧边栏小工具',
  61.         'class'         => 'custom',
  62.         'before_widget' => '<div id="%1$s" class="widget %2$s">',
  63.         'after_widget'  => '</div>',
  64.         'before_title'  => '<div class="widget-title">',
  65.         'after_title'   => '</div>'
  66.     );
  67.     register_sidebar($args);
  68. }
  69. add_action('widgets_init', 'widgetSetup');
也想出现在这里?联系我们
创客主机

以上的代码包括了首页,文章页,指定一个页面的侧边栏,同时每个侧边栏都包括一个不随滚动条滚动的边栏,在主题 sidebar.php 替换为以下代码:

  1. <?php
  2. /**
  3.  * The template for the sidebar containing the main widget area
  4.  */
  5.  
  6. ?>
  7. <aside id="sidebar">
  8. 	<div class="sidebar-wrap">
  9.         <?php if (!is_active_sidebar('sidebar-index') && !is_active_sidebar('sidebar-index-affix') && !is_active_sidebar('sidebar-article') && !is_active_sidebar('sidebar-article-affix')): ?>
  10.             <div class="widget"><p>请到[后台->外观->小工具]首页或文章页侧边栏中添加需要显示的小工具。</p></div>
  11.         <?php else: ?>
  12.             <?php if (is_home()): ?>
  13.                 <div class="affix">
  14.                     <?php dynamic_sidebar('sidebar-index-affix'); ?>
  15.                 </div>
  16.                 <div class='sidebar-index wow slideInUp'>
  17.                     <?php dynamic_sidebar("sidebar-index"); ?>
  18.                 </div>
  19.             <?php endif; ?>
  20.             <?php if (is_single()): ?>
  21.                 <div class="affix">
  22.                     <?php dynamic_sidebar('sidebar-article-affix'); ?>
  23.                 </div>
  24.                 <div class='sidebar-article wow slideInUp'>
  25.                     <?php dynamic_sidebar("sidebar-article"); ?>
  26.                 </div>
  27.             <?php endif; ?>
  28.             <?php if (is_page(1003)): ?>
  29.                 <div class="affix">
  30.                     <?php dynamic_sidebar('sidebar-page-affix'); ?>
  31.                 </div>
  32.                 <div class='sidebar-page wow slideInUp'>
  33.                     <?php dynamic_sidebar("sidebar-page"); ?>
  34.                 </div>
  35.             <?php endif; ?>
  36.         <?php endif; ?>
  37. 	</div>
  38. </aside>

这里包括了固定栏的 affix,通过 JS 和 css 便能弄出各种样式了,设置这些后,找到:外观-小工具,就能看到这些侧边栏了的 JS

滚动超出侧边栏高度时,固定侧边栏:

  1. var headerH = $('#header').height();
  2. var footerH = $('#footer').innerHeight();
  3. var windowH = $(window).height();
  4. var sidebarW = $('#sidebar').width();
  5. var sidebarH = $('#sidebar').outerHeight();
  6. var sidebarTop = $('#sidebar').offset().top;
  7. $(window).scroll(function(event) {
  8.     var bodyH = $(document).height();
  9.     var affixH = $(".affix").innerHeight();
  10.     var leftH = (windowH - headerH - affixH) > 0 ? (windowH - headerH - affixH) : 0;
  11.     var scrollTop = $(document).scrollTop();
  12.     var scrollBottom = bodyH - windowH - scrollTop;
  13.         if (scrollTop > sidebarTop+ sidebarH) {
  14.             if (scrollTop < (bodyH - footerH - windowH + leftH)) {
  15.                 $('.affix').css({
  16.                     position: 'fixed',
  17.                     top: $('#header').height()+$('#header').position().top+3,
  18.                     bottom: '',
  19.                     width: sidebarW + 'px'
  20.                 });
  21.             } else {
  22.                 $('.affix').css({
  23.                     position: 'fixed',
  24.                     top: '',
  25.                     bottom: footerH - scrollBottom,
  26.                     width: sidebarW + 'px',
  27.                 });
  28.             }
  29.         } else {
  30.             $('.affix').css({
  31.                 position: '',
  32.                 top: '',
  33.                 width: sidebarW + 'px',
  34.             });
  35.         }
  36. }

这滚动的处理,得用好多参数呀,看着都头晕,是否还有更简捷的处理方式呢,欢迎分享

WordPress 首页文章页或其它自定义侧边栏

已有 482 人购买
查看演示升级 VIP立刻购买

收藏
(3)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!