PHP script to implement WordPress theme Sidebar Switch function _php instance

Source: Internet
Author: User
As the creator of the theme, in addition to the implementation of functions, display interface, but also the responsibility to make the theme flexible, to meet the needs of more people different.
Maybe some friends have bothered to choose a double-column theme (one-sided sidebar) or a three-column theme (two-sided sidebar). Let's take a Classic theme and talk about how to easily switch between one-sided sidebar and two-sided sidebar in a theme. Finally I will provide the revised theme.

Add Management options
Background processing
First of all, we want to modify the function.php, the main processing work in this file, if the topic does not have this file, create a bar. (There is no function.php the theme does not support widgets, is not a good habit, oh, or quickly create a new bar)
My handling includes 3 chunks: Get options, initialize, TAB interface. Only one bulletin board is created here, including two options (whether the announcement bar and bulletin board content is displayed). If you want to add more options, you just need to append some code to the 3 TODO locations in your code. Of course, you'll also need to change the name of the option to replace both classic and classic.

<?php/** * option group type */class classicoptions {/*--get option group--*/function GetOptions () {//Get option group in database $options = Get_optio N (' classic_options ');  If the option group does not exist in the database, set the default values for these options and insert them into the database if (!is_array ($options)) {$options [' notice '] = false;  $options [' notice_content '] = '; TODO: Append additional options here update_option (' classic_options ', $options); }//Return option group return $options; }/*--initialize--*/function init () {//If it is a POST submission data, restrict the data and update to the database if (isset ($_post[' Classic_save ')) {//Get the option group because there are   It is possible to modify only some of the options so that the whole is taken down and then changed $options = Classicoptions::getoptions ();  Data limit if ($_post[' notice ']) {$options [' notice '] = (bool) true;  } else {$options [' notice '] = (bool) false;   } $options [' notice_content '] = stripslashes ($_post[' notice_content ');  TODO: In this additional option the limit processing//Update data update_option (' classic_options ', $options);  Otherwise, retrieve the option group, which is the initialization of the data} else {classicoptions::getoptions ();} Append a tab to the background Design page called the current Theme options add_theme_page (' Current Theme options ', ' current Theme OPtions ", ' Edit_themes ', basename (__file__), Array (' classicoptions ', ' Display ')); }/*--Label page-*/function display () {$options = Classicoptions::getoptions ();? > <?php}}/** * Register initialization Method */add_action (' Admin_menu ', Array (' classicoptions ', ' init '));?>

Foreground processing

To the bulletin board on the first page to display, need to modify the index.php, this is relatively simple, just through a few judgment statements to determine whether things should not show it. Of course, you can do other things, the key is to get the values of the options and manipulate them.
In fact, it can be divided into two steps:

Get options (for each PHP file, get once on the line, can be executed at the top of the file)
Process the options (the announcement will be displayed if the judgment is established here)

 
  <?php $options = get_option (' classic_options ');?> 
 
   <?php if ($options [' notice '] && $options [ ' Notice_content '):?>  <?php Echo ($options [' notice_content ']);?> <?php endif;?>

You can use administrative items to control the number of sidebar, get the number of sidebar in the theme file, and make different processing for different quantities to achieve the purpose of switching between the different number of sidebar.

The number of sidebar, the default is one-sided sidebar $options[' sidebar ' = 1;//get the latest submitted value $options[' Sidebar '] = $_post[' sidebar '); 
  
   <?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

Because we want to switch between the one-sided sidebar and the two-sided sidebar, we need to define two Widget-initialized branches for each of the two different modes.
This is especially true, in order to get the Widget information correctly in the code, even the one-sided sidebar requires an alias. Just like the Sidebar_single in the code. When the number of sidebar is 1 o'clock, register Sidebar_single. When the number of sidebar is 2 o'clock, register Sidebar_top and Sidebar_bottom.

Widgets$options = get_option (' classic_options '); One-sided sidebar if (function_exists (' Register_sidebar ') && $options [' sidebar '] = = 1) {Register_sidebar (Array (' name ' = > ' Sidebar_single ', ' before_widget ' = '
  • ', ' after_widget ' = '
  • ', ' before_title ' = '

    ', ' after_title ' = '

    ' )); Two-sided Sidebar} else if (function_exists (' Register_sidebar ') && $options [' sidebar '] = = 2) {Register_sidebar (Array ( ' name ' = ' sidebar_bottom ', ' before_widget ' = '
  • ', ' after_widget ' = '
  • ', ' before_title ' = '

    ', ' after_title ' = '

    ' )); Register_sidebar (Array ( ' name ' = ' sidebar_top ', ' before_widget ' = '
  • ', ' after_widget ' = '
  • ', ' before_title ' = '

    ', ' after_title ' = '

    ' ));}

    Modify the sidebar structure

    First of all make it clear that we now need a two-sided sidebar structure. How do you turn the two-sided sidebar into a one-sided sidebar? As soon as you delete the Closing tab of the previous sidebar and the Start tab of the latter sidebar, the two sidebar is merged into one sidebar. Simple text is difficult to express my ideas and implementation, you can continue to look at the following code and sample pictures.

     
      
      
      <?php if (!function_exists (' Dynamic_sidebar ') | |!dynamic_sidebar (' sidebar_single '))://Single?> <?php if (!function_exists (' Dynamic_sidebar ') | |!dynamic_sidebar (' sidebar_top '))://Top?> <?php endif; Top?> <?php if ($options [' Sidebar '] >= 2):?>
      <?php endif;?> & lt;? PHP if (!function_exists (' Dynamic_sidebar ') | |!dynamic_sidebar (' sidebar_bottom '))://Bottom?> <?p HP endif; Bottom?> <?php endif;//single?>

    OK, this is the sidebar code structure. It is perfect for switching between single and double side bars. But how does it work? I'll use the picture to List 6 of its possible statuses later.
    Because the theme already supports widgets, the Code function_exists (' dynamic_sidebar ') = = = True, then!function_exists (' dynamic_sidebar ') = = = False.
    Remember the code you wrote when you added Widget support? The sidebar is 1 o'clock sidebar_single effective, the sidebar is 2 o'clock, sidebar_top and Sidebar_bottom are valid. This is the key to running through the whole idea.

    Note:

      • Red: Indicates that the value of the selected code is false and does not pass
      • Green: Indicates that the value of the selected code is true, by
      • Blue: Indicates that the selected part will be replaced by the chosen widgets
      • Gray: Indicates that the selected part of the code will be invalidated

    Status one: one-sided sidebar with no widgets

    Status two: Two-sided sidebar, no widgets used

    Status three: one-sided sidebar, using widgets

    Status four: Two-sided sidebar, top sidebar using widgets

    Status five: Two-sided sidebar, bottom sidebar using widgets

    Status six: Two-sided sidebar, both top and bottom sidebar use widgets

  • Related Article

    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.