侧边栏广告位固定

<aside>  //以下是wordpress侧边栏的一个aside代码
    <div id="nav_keleyi_com" style="position:relative;top:0;width:160px; height:600px">    //width和height是本需要展示div的实际宽和高
        <!-- Begin BidVertiser code -->
        <SCRIPT data-cfasync="false" SRC="//bdv.bidvertiser.com/BidVertiser.dbm?pid=721581&bid=2049940" TYPE="text/javascript"></SCRIPT>
        <!-- End BidVertiser code -->
    </div>
    <script type="text/javascript">
        function menuFixed(id) {
            var obj = document.getElementById(id);
            var _getHeight = obj.offsetTop;

            window.onscroll = function () {
                changePos(id, _getHeight);
            }
        }

        function changePos(id, height) {
            var obj = document.getElementById(id);
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            if (scrollTop < height) {
                obj.style.position = 'relative';
            } else {
                obj.style.position = 'fixed';
            }
        }
        window.onload = function () {
            menuFixed('nav_keleyi_com');
        }
    </script>
</aside>
wordpress sidebar.php的完整源码如下:
<?php
/**
 * The sidebar containing the main widget area
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package brisko
 */

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
	return;
} ?>
<aside id="secondary" class="widget-area">
	<?php brisko_before_sidebar(); ?>
	<?php dynamic_sidebar( 'sidebar-1' ); ?>
	<?php brisko_after_sidebar(); ?>
</aside><!-- #secondary -->
<aside>
    <div id="nav_keleyi_com" style="position:relative;top:0;width:160px; height:600px">
        <!-- Begin BidVertiser code -->
        <SCRIPT data-cfasync="false" SRC="//bdv.bidvertiser.com/BidVertiser.dbm?pid=721581&bid=2049940" TYPE="text/javascript"></SCRIPT>
        <!-- End BidVertiser code -->
    </div>
    <script type="text/javascript">
        function menuFixed(id) {
            var obj = document.getElementById(id);
            var _getHeight = obj.offsetTop;

            window.onscroll = function () {
                changePos(id, _getHeight);
            }
        }

        function changePos(id, height) {
            var obj = document.getElementById(id);
            var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
            if (scrollTop < height) {
                obj.style.position = 'relative';
            } else {
                obj.style.position = 'fixed';
            }
        }
        window.onload = function () {
            menuFixed('nav_keleyi_com');
        }
    </script>
</aside>

示例二:

<?php
/**
 * The sidebar containing the main widget area.
 *
 * @package Gridbox
 */

// Check if Sidebar has widgets.
if ( is_active_sidebar( 'sidebar' ) ) :
?>
    <section class="sidebar widget-area clearfix" role="complementary">
        <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7145011761360998"
                crossorigin="anonymous"></script>
        <!-- themes.show右侧正方形广告 -->
        <ins class="adsbygoogle"
             style="display:block"
             data-ad-client="ca-pub-7145011761360998"
             data-ad-slot="5093541512"
             data-ad-format="auto"
             data-full-width-responsive="true"></ins>
        <script>
            (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
    </section>
	<section id="secondary" class="sidebar widget-area clearfix" role="complementary" style="margin-top: 20px">

		<?php dynamic_sidebar( 'sidebar' ); ?>

	</section>
    <section  class="sidebar widget-area clearfix" role="complementary">
        <div id="nav_keleyi_com" style="position:relative;top:0;width:360px; height:600px">
            <div class="widget-header"><h3 class="widget-title">RANDOM ARTICLE</h3></div>
            <ul>
                <?php
                $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );
                $rand_posts = get_posts( $args );
                foreach( $rand_posts as $post ) : ?>
                    <li><a style="color: #000000" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach; ?>
            </ul>
            <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7145011761360998"
                    crossorigin="anonymous"></script>
            <!-- themes.show右侧纵向广告 -->
            <ins class="adsbygoogle"
                 style="display:block"
                 data-ad-client="ca-pub-7145011761360998"
                 data-ad-slot="9184625534"
                 data-ad-format="auto"
                 data-full-width-responsive="true"></ins>
            <script>
                (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        </div>
        <script type="text/javascript">
            function menuFixed(id) {
                var obj = document.getElementById(id);
                var _getHeight = obj.offsetTop;

                window.onscroll = function () {
                    changePos(id, _getHeight);
                }
            }

            function changePos(id, height) {
                var obj = document.getElementById(id);
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                if (scrollTop < height) {
                    obj.style.position = 'relative';
                } else {
                    obj.style.position = 'fixed';
                }
            }
            window.onload = function () {
                menuFixed('nav_keleyi_com');
            }
        </script>
	</section>

<?php endif; ?>