This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Topic based

Use the functions.php file to add unique functionality to the theme.

Functions.php is similar to a plug-in.

WordPress plugin:

  • Requires specific, unique title text;
  • Stored in wP-Content/plugins, usually in subdirectories;
  • Only at activation time when the page loads;
  • Applicable to all topics; and
  • There should be a purpose – for example, to provide search engine optimization or help with backups.

Meanwhile, a functions.php file:

  • You don’t need unique header text;
  • Stored in the wp-Content/Themes subdirectory of the theme;
  • Execute only in the directory of the active subject;
  • Only for the topic (if you change the topic, you can no longer use these features);
  • There can be many blocks of code for many different purposes.

Navigation menu

Register a menu with register_nav_menus. ,

register_nav_menus( array(
    'primary'   => __( 'Primary Menu', 'myfirsttheme' ),
    'secondary' => __( 'Secondary Menu', 'myfirsttheme' )
) );
Copy the code

You can use wP_nav_menu to use the registered menu.

internationalization

Multiple languages can be translated using load_theme_textDomain.

load_theme_textdomain( 'myfirsttheme', get_template_directory() . '/languages' );
Copy the code

The content width

Set the maximum width of the content area to 800 pixels.

if(! isset($content_width)){ $content_width = 800; }Copy the code

Link topic file

Call using a template tag.

get_header, get_footer, get_sidebar

If there is a custom version file: sidebar-{name}.php, header-{name}.php

Get_header (‘name’)

You can also call them anywhere using get_template_part.

A template file content.php is created

Add specific content to content-product.php

Get_template_part (‘content’, ‘product’)

Theme directory

Get_theme_file_uri Gets the theme directory

The parameter is the file on the path. The current theme is preferred. If there is no file, go to the parent theme.

In the subtheme, use get_parent_theme_file_URI (), get_parent_theme_file_path() to get the URI or path.

Dynamic link

Use get_permalink($Id) to get the link.

Insert script and style

Use wP_enqueue_script or WP_enqueue_style.

Load the style

wp_enqueue_style( 'style', get_stylesheet_uri() );
Copy the code
wp_enqueue_style( $handle, $src, $deps, $ver, $media);
Copy the code
  • $Handle is just the name of the stylesheet.
  • $SRC is where it is. The rest of the parameters are optional.
  • $deps indicates whether this style sheet depends on another style sheet. If this option is set, the style sheet will not be loaded unless the style sheet it depends on is loaded first.
  • $ver Sets the version number.
  • $media can specify the media type to load this stylesheet, such as “all,” “screen,” “Print,” or “handheld.”

The theme’s root directory is called slider.css

Wp_enqueue_style ('slider', get_template_directory_uri(). '/ CSS /slider.css',false,'1.1','all');Copy the code

Load scripts

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer);
Copy the code
  • $handle is the name of the script.
  • $SRC defines the location of the script.
  • $deps is an array that can handle any script the new script depends on, such as jQuery.
  • $ver lets you list version numbers.
  • $in_footer is a Boolean parameter (true/false) that allows you to place the script in the footer of your HTML document, rather than in the title, so as not to delay loading the DOM tree.

Combine all queued loads into a function.

Use an action to add_action(‘wp_enqueue_scripts’, ‘add_theme_scripts’)

function add_theme_script(){

}

Conditions of the template

Log in to is_user_logged_in()

Is it on the home page is_home()

Is it in the page? Is_front_page ()

Is the management industry is_admin()

The is_single() argument is the title or ID of the post. The argument can be an array. The number is the ID and the string is the title.

Get_post_type () gets the type of the current post.

When is_Page () displays any page, it displays the ID as a number, the string as the title, and it can be an array.

Is_category category

Is_tag label

Is_author author