PHP script details for implementing the WordPress topic sidebar switch function _ php instance

Source: Internet
Author: User
This article mainly introduces the PHP script for implementing the WordPress topic sidebar switch function, including how to add management options for the WordPress topic. If you need a friend, refer to it as the topic producer, in addition to implementing functions and displaying interfaces, there is also a responsibility to make theme flexible and changeable to meet different needs of more people.
Some friends may have worried about the double-column topic (single sidebar) or the three-column topic (double sidebar. next we will take the Classic topic as an example to talk about how to easily switch between a single sidebar and a double sidebar in the topic. finally, I will provide the modified theme.

ADD management options
Background Processing
First, we need to modify the function. php, the main processing work is in this file. If the topic does not have this file, create one. (no function. php indicates that the theme does not support widgets, which is not a good habit. Please create a new theme)
My processing includes three parts: Getting options, initializing, and Tab operation interface. only one announcement board is created here, which includes two options (whether to display the announcement board and announcement board content ). to add more options, you only need to append some code to the three TODO locations in the code. of course, you also need to change the option name to replace Classic and classic.

<? Php/*** sequence group type */class ClassicOptions {/* -- get sequence group -- */function getOptions () {// obtain the sequence group $ options = get_option ('classic _ options') in the database; // If the sequence group does not exist in the Database, set the default values of these options, and insert them into the database if (! Is_array ($ options) {$ options ['notice '] = false; $ options ['notice _ content'] = ''; // TODO: append other options update_option ('classic _ options', $ options);} // return the response group return $ options;}/* -- initialize -- */function init () {// if the data is submitted by POST, restrict the data and update it to the database if (isset ($ _ POST ['classic _ save']) {// obtain the sequence group, because it is possible to modify only some options, take the entire option and then change $ options = ClassicOptions: getOptions (); // data restriction if ($ _ POST ['notice']) {$ options ['Notice'] = (bool) true;} else {$ options ['notice'] = (bool) false ;} $ options ['notice _ content'] = stripslashes ($ _ POST ['notice _ content']); // TODO: append other options here for processing restrictions // update_option ('classic _ options', $ options) for updating data; // otherwise, obtain the quota group again, that is, initialize the data} else {ClassicOptions: getOptions () ;}// append a tab on the background Design page, called Current Theme Options add_theme_page ("Current Theme Options ", "Current Theme Options", 'edi T_themes ', basename (_ FILE _), array ('classicoptions', 'display');}/* -- tab -- */function display () {$ options = ClassicOptions: getOptions ();?> <? Php }}/ *** registration Initialization Method */add_action ('admin _ menu ', array ('classicoptions', 'init');?>

Frontend Processing

To display the announcement board on the home page, you need to modify the index. php, this is relatively simple. It is only through some judgment statements that determine whether something should be displayed. of course, you can perform other operations. The key is to get the options and process them.
There are two steps:

Obtain the option (for each PHP file, you can obtain it once and execute it at the top of the file)
Process the options (the announcement content will be displayed if it is determined to be true)

 <? Php $ options = get_option ('classic _ options');?>
 <? Php if ($ options ['notice'] & $ options ['notice _ content']):?>

<? Php echo ($ options ['notice _ content']);?>

<? Php endif;?>

You can use management items to control the number of sidebar, obtain the number of sidebar in the topic file, and process different quantities to switch between different quantities of sidebar.

// Number of sidebar. The default value is single sidebar $ options ['inclubar'] = 1; // obtain the latest submitted value $ options ['inclubar'] = $ _ POST ['inclubar']; 
   <?php if($options['sidebar'] !="2)" echo ' selected '; ?>> <? Php _ e ('single ', 'classic');?> 
   <?php if($options['sidebar']="=" 2) echo ' selected '; ?>> <? Php _ e ('double', 'classic ');?><? Php _ e ('sidebar (s) ', 'classic');?>.

Add Widget support

To switch between a single sidebar and a double sidebar, we need to define the branches for initialization of two widgets for different two modes.
This is special. In order to get the Widget information correctly in the code, an alias is required even for a single sidebar. like Sidebar_single in the code. when the number of Sidebar is 1, register Sidebar_single. when the number of Sidebar is 2, register Sidebar_top and Sidebar_bottom.

// Widgets $ options = get_option ('classic _ options'); // if (function_exists ('register _ sidebar ') & $ options ['inclubar'] = 1) {register_sidebar (array ('name' => 'inclubar _ sing', 'before _ components' =>'
  • ', 'After _ components' =>'
  • ', 'Before _ title' => '', 'after _ title' =>''); // double sidebar} else if (function_exists ('register _ sidebar ') & $ options ['includebar'] = 2) {register_sidebar (array ('name' => 'includebar _ bottom', 'before _ components' =>'
  • ', 'After _ components' =>'
  • ', 'Before _ title' => '', 'after _ title' =>''); register_sidebar (array ('name' => 'sidebar _ top ', 'Before _ components' =>'
  • ', 'After _ components' =>'
  • ', 'Before _ title' => '', 'after _ title' => ''));}

    Modify sidebar Structure

    First of all, we need to clarify the structure of the double sidebar. How can we change the double sidebar into a single sidebar? You only need to delete the end label of the previous sidebar and the start label of the next sidebar, and then combine the two sidebar into a sidebar. it is difficult to express my thoughts and implementations in simple words. You can continue to look at the following code and sample images.

     
      <? Php if (! Function_exists ('dynamic _ sidebar') |! Dynamic_sidebar ('sidebar _ sing'): // single?> <? Php if (! Function_exists ('dynamic _ sidebar') |! Dynamic_sidebar ('inclubar _ top'): // top?> <? Php endif; // top?> <? Php if ($ options ['inclubar']> = 2):?>
      <? Php endif;?> <? Php if (! Function_exists ('dynamic _ sidebar') |! Dynamic_sidebar ('sidebar _ bottom '): // bottom?> <? Php endif; // bottom?> <? Php endif; // single?>

    OK, this is the code structure of the sidebar. It can perfectly implement switching between Single and Double sidebar. But how does it work? I will use an image later to list the six possible statuses.
    Because the topic already supports widgets, in the Code, function_exists ('dynamic _ sidebar') = true, then! Function_exists ('dynamic _ sidebar') = false.
    Do you remember the code written when you added support for widgets? When the sidebar is 1, sidebar_single is valid. When the sidebar is 2, sidebar_top and sidebar_bottom are valid. This is the key to the entire idea.

    Note:

    • RED: indicates that the value of the selected code is false.
    • Green: indicates that the value of the selected code is true.
    • Blue: indicates that the selected part will be replaced by the selected widgets.
    • Gray: indicates that some of the selected code will be invalid.

    Status 1: single sidebar, no widgets used

    Status 2: Double sidebar, no widgets used

    Status 3: single sidebar, using Widgets

    Status 4: Double sidebar, and Widget on the top sidebar

    Status 5: Double sidebar. widgets are used in the bottom sidebar.

    Status 6: Double sidebar. widgets are used for the top and bottom sidebar.

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.