Traditional Culture Encyclopedia - Travel guide - How to add a theme to a wordpress single page

How to add a theme to a wordpress single page

1. Create the required files

Before theme customization, you should first create a customizable "Settings Options Page". The code to create the settings options page needs to be placed in the functions.php file in the theme directory. If our template is named "OptionPage", then the path to the functions file is: wp-contentthemesOptionPagefunctions.php.

We don’t need to ask wordpress to load it manually, wordpress will load it automatically when executing.

2. Create a setting options page

First of all, the first step is to create a blank page in the background for us to use. We implement this step through add_aaction. Actions can be executed at specific times when WordPress is executing. For example, when a menu is created in the control panel, admin_menu will be executed in response. Therefore, these can be leveraged to achieve the functionality we require. This is the most basic function for us to create options pages.

// Set options page

function themeoptions_admin_menu()

{

// Add a settings options page link in the sidebar of the control panel

add_theme_page("Theme Settings", "Theme Options", 'edit_themes', basename(__FILE__), 'themeoptions_page');

< p>}

function themeoptions_page()

{

// Set the main function of the options page

}

< p>add_action('admin_menu', 'themeoptions_admin_menu'); >

themeoptions_admin_menu() adds a link in the sidebar of the control panel, pointing to the options page we created: themeoptions_page.

The parameters of add_theme_page() are:

Page title: Theme settings

Menu title: Theme options (p.s. In order to distinguish the display, I made the page and menu titles (named differently)

Function: edit_themes;

Handle (handle): current file;

Function executed: themeoptions_page;

< p>Now there is an additional "Theme Settings" menu in the sidebar of the background control panel, but it is still blank. The customized content we want to implement later is created on this blank page.

3. Add options and fields

Now we can add our options and fields to the blank page we just created. You can style this page according to your needs, but for this tutorial we will use the default WordPress classes, which saves us time and looks more native.

The code for page content needs to be included in the themeoptions_page() function. First, we add a div container with class="wrap"; then, add a default icon in the header as the page title; and finally, design the form.

In the form, first we need to add a hidden value through which we can check whether the update Already submitted. Then add a submit button. Here I also use WordPress’s default button style.