Home >Backend Development >PHP Tutorial >Explain the PHP functions used to obtain comment templates and search forms in WordPress, wordpressphp_PHP tutorial
comments_template() (get comment template)
The comments_template() function is used to obtain the comment template. Generally it can only be used on articles or pages. If it is not an article or page, it will not be displayed.
Usage
comments_template( $file, $separate_comments );
Parameters
$file
(string) (optional) Template file to comment on.
Default value: /comments.php (the comments.php file in the root directory of the current theme).
$separate_comments
(boolean) (optional) Whether to differentiate comments based on their type.
Default value: False
Return value
This function has no return value.
Example
The comments.php file in the root directory of the current theme is imported by default.
<?php comments_template(); ?>
Introduce custom files:
<?php comments_template( '/short-comments.php' ); ?>
Others
This function is located at: wp-includes/comment-template.php
get_search_form() (get search form)
The get_search_form() function is used to obtain the search form. The code of the search form is located in the searchform.php file in the root directory of the current theme.
Usage
get_search_form( $echo );
Parameters
$echo
(Boolean) (optional) If true, print the search form directly, if not, return the code of the search form.
Default value: True
Return value
(string) If the $echo parameter is False, returns the Html code of the search form.
Example
If there is no searchform.php file in the theme root directory, the form code below will default to:
<form role="search" method="get" id="searchform" class="searchform" action="<?php esc_url( home_url( '/' )); ?>"> <div> <label class="screen-reader-text" for="s"><?php _x( 'Search for:', 'label' ); ?></label> <input type="text" value="<?php get_search_query(); ?>" name="s" id="s" /> <input type="submit" id="searchsubmit" value="<?php esc_attr_x( 'Search', 'submit button' ); ?>" /> </div> </form>
Others
This function is located at: wp-includes/general-template.php