如何为wordpress主题制作右侧边栏

时间:2015-10-15 12:53:52

标签: php html css wordpress wordpress-theming

如何为wordpress主题制作右侧边栏并与高级搜索集成,如下所示: right sidebar concept

的sidebar.php

package com.xyz.util;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;
import android.os.Handler;
import android.util.Log;

public class MemCheck
{
    private static final boolean                enabled = true;
    private static final int                    periodic = 0; // seconds, 0 == disabled
    private static final String                 tag = MemCheck.class.getName();
    private static TreeMap<String, RefCount>    mObjectMap = new TreeMap<String, RefCount>(); 
    private static Runnable                     mPeriodicRunnable = null;
    private static Handler                      mPeriodicHandler = null;

    public static void add( Object object )
    {
        if( !enabled )
            return;

        synchronized( mObjectMap )
        {
            String name = object.getClass().getName();
            RefCount queue = mObjectMap.get( name );
            if( queue == null )
            {
                queue = new RefCount();
                mObjectMap.put( name, queue );
                queue.add( object );
            }
            else
                queue.add( object );
        }
    }

    public static void countAndLog()
    {
        if( !enabled )
            return;
        System.gc();
        Log.d( tag, "Log report starts" );
        Iterator<Entry<String, RefCount>> entryIter = mObjectMap.entrySet().iterator();
        while( entryIter.hasNext() )
        {
            Entry<String, RefCount> entry = entryIter.next(); 
            String name = entry.getKey();
            RefCount refCount = entry.getValue();
            Log.d( tag, "Class " + name + " has " + refCount.countRefs() + " objects in memory." );
        }

        logMemoryUsage();
        Log.d( tag, "Log report done" );
    }

    public static void logMemoryUsage()
    {
        if( !enabled )
            return;
        Runtime runtime = Runtime.getRuntime();
        Log.d( tag, "Max Heap: " + runtime.maxMemory() / 1048576 + " MB, Used: " + runtime.totalMemory() / 1048576 +
               " MB, Free: " + runtime.freeMemory() / 1024 + " MB" );

        if( periodic > 0 )
        {
            if( mPeriodicRunnable != null )
                mPeriodicHandler.removeCallbacks( mPeriodicRunnable );
            if( mPeriodicHandler == null )
                mPeriodicHandler = new Handler();
            mPeriodicRunnable = new Runnable()
            {
                @Override
                public void run()
                {
                    mPeriodicRunnable = null;
                    countAndLog();
                    logMemoryUsage(); // this will run the next
                }
            };

            mPeriodicHandler.postDelayed( mPeriodicRunnable, periodic * 1000 );
        }
    }

    private static class RefCount
    {
        private ReferenceQueue<Object>  mQueue = new ReferenceQueue<Object>();
        private HashSet<Object>         mRefHash = new HashSet<Object>(); 
        private int                     mRefCount = 0;

        public void add( Object o )
        {
            synchronized( this )
            {
                mRefHash.add( new PhantomReference<Object>( o, mQueue ) ); // Note: References MUST be kept alive for their references to be enqueued
                mRefCount++;
            }
        }

        public int countRefs()
        {
            synchronized( this )
            {
                Object ref;
                while( ( ref = mQueue.poll() ) != null )
                {
                    mRefHash.remove( ref );
                    mRefCount--;
                }

                return mRefCount;
            }
        }

    }
}

侧栏的style.css

    <?php
/**
 * The sidebar containing the main widget area.
 *
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package custom
 */

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}
?>

<div id="secondary" class="widget-area" role="complementary">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary -->
<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

     <div id="tertiary" class="widget-area" role="supplementary">
      <?php dynamic_sidebar( 'sidebar-2' ); ?>
     </div><!-- #secondary .widget-area -->

<?php endif; ?>

我如何以一种简单的方式实现它?

2 个答案:

答案 0 :(得分:0)

有关自定义侧边栏的信息,请查看WordPress Codex。此页面上有关于侧栏的大量信息:https://codex.wordpress.org/Customizing_Your_Sidebar

如果您正在询问如何从屏幕一侧扩展菜单,那么有许多不同的CSS / JavaScript实现,但也许像这样的jQuery Slick插件可能会有所帮助:http://www.designchemical.com/lab/jquery-slick-plugin/examples/

如果您询问如何创建自定义搜索功能,那么这将非常依赖于您正在使用的插件以及您要搜索的内容。本文确实概述了在WordPress中开始使用自定义搜索功能和自定义搜索表单:https://premium.wpmudev.org/blog/build-your-own-custom-wordpress-search/

答案 1 :(得分:0)

如果您已在模板文件中设置了sidebar.php所需的位置;它可以是index.phpsingle.phppage.php或其他

我认为有两种方法可以做到这一点

1 - 安装一些精美的搜索插件,并使用信息中心 - 外观 - 窗口小部件区域

将其添加到侧边栏

2 - 在窗口小部件区域中,您可以添加 Html内容,您可以在其中使用搜索表单或在sidebar.php中进行硬编码或创建{{1 (您将拥有自己的表单)并将其包含在search-form.php中或使用sibedar.php

直接包含在模板文件中

这里有一些有用的链接:

https://codex.wordpress.org/Function_Reference/get_search_form https://codex.wordpress.org/Function_Reference/get_template_part