Home > Backend Development > PHP Tutorial > Analysis of related functions used for title display in WordPress development, wordpress title_PHP tutorial

Analysis of related functions used for title display in WordPress development, wordpress title_PHP tutorial

WBOY
Release: 2016-07-12 09:01:17
Original
823 people have browsed it

Usage analysis of related functions for title display in WordPress development, wordpress title

single_cat_title() function
The single_cat_title() function is rarely used in daily life, but this function will solve many problems for us, such as the directory and tags of the current page. This function is not attached to the WordPress main loop, nor can it be placed in the main loop. used in loops.

Description
Get the categories and tags of the current page.

 <&#63;php single_cat_title($prefix,$display); &#63;>

Copy after login
  • $prefix: used to set the content displayed before the title.
  • $display: used to set whether to display directly or return to a variable.

Example
Here is an excerpt of the code around line 18 of the category.php file in the WordPress 2011 default theme

 <&#63;php
printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
&#63;>

Copy after login

get_the_title and the_title
The two functions get_the_title and the_title are used to display the article title on the article page. The reason why the two functions are merged into one article is because these two functions are implemented in the same way, but the_title is displayed directly by default. get_the_title returns a string by default. If you have any doubts about this, please read below.

Detailed explanation of functions
The two functions get_the_title and the_title are mainly used to display the title of the current article in a loop. Please note that the_title function must be used in a loop.
The difference between the two is that get_the_title can only return the article title in the form of a string, while the_title can set custom characters before and after the title, and whether to display or return a string.

the_title function usage and detailed explanation of parameters

<&#63;php the_title( $before, $after, $echo ); &#63;>
Copy after login
  • Characters before $before title
  • $afterThe characters after the title
  • $echo displays or returns a string, the default is true

the_title example

<&#63;php the_title( ‘=>', ‘<=' ); &#63;>
Copy after login

Take this article as an example, we will get the following title:

‘=>get_the_title 和 the_title<='

Copy after login

get_the_title function usage and detailed explanation of parameters

<&#63;php $myTitle = get_the_title($ID); &#63;>
Copy after login

With the above code we will get the variable $myTitle of the article title;
$ID is used to set the article ID. Of course, we can omit this parameter in the loop.

get_the_title example

<&#63;php
 $myTitle = get_the_title($ID); 
 echo $mytitle.'【标题演示】';
&#63;>
Copy after login

We will get

get_the_title and the_title【Title Demo】

Summary
Having said so much, I wonder if it is helpful to you?
In general, the_title is a higher-level encapsulation of get_the_title. As mentioned in wp_title, higher-level encapsulation, although simple to use, has relatively few tricks.
The following is the source code of the two functions

the_title function declaration
This function is located around lines 43 – 55 of the wp-include/post-template.php file

<&#63;php
/**
 * Display or retrieve the current post title with optional content.
 *
 * @since 0.71
 *
 * @param string $before Optional. Content to prepend to the title.
 * @param string $after Optional. Content to append to the title.
 * @param bool $echo Optional, default to true.Whether to display or return.
 * @return null|string Null on no title. String if $echo parameter is false.
 */
function the_title($before = '', $after = '', $echo = true) {
 $title = get_the_title();
 
 if ( strlen($title) == 0 )
 return;
 
 $title = $before . $title . $after;
 
 if ( $echo )
 echo $title;
 else
 return $title;
}
&#63;>
Copy after login

get_the_title function declaration
This function is located around lines 103 – 118 of the wp-include/post-template.php file

<&#63;php
/**
 * Retrieve post title.
 *
 * If the post is protected and the visitor is not an admin, then "Protected"
 * will be displayed before the post title. If the post is private, then
 * "Private" will be located before the post title.
 *
 * @since 0.71
 *
 * @param int $id Optional. Post ID.
 * @return string
 */
function get_the_title( $id = 0 ) {
 $post = &get_post($id);
 
 $title = isset($post->post_title) &#63; $post->post_title : '';
 $id = isset($post->ID) &#63; $post->ID : (int) $id;
 
 if ( !is_admin() ) {
 if ( !empty($post->post_password) ) {
  $protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
  $title = sprintf($protected_title_format, $title);
 } else if ( isset($post->post_status) && 'private' == $post->post_status ) {
  $private_title_format = apply_filters('private_title_format', __('Private: %s'));
  $title = sprintf($private_title_format, $title);
 }
 }
 return apply_filters( 'the_title', $title, $id );
}
&#63;>
Copy after login

Articles you may be interested in:

  • Related PHP functions for debugging thumbnails in WordPress use parsing
  • Configuration to solve the problem of WordPress paths not automatically adding slashes in the Nginx server
  • Usage analysis of the PHP function used to obtain the search form in WordPress
  • Use the wp_count_posts function in WordPress to count the number of articles
  • Detailed explanation of calling comment templates and looping output comments in WordPress PHP functions
  • Detailed explanation of the use of classification function wp_list_categories in WordPress
  • WordPress restricts non-admin users to only comment once after an article
  • Detailed explanation of creating and adding filters in WordPress Related PHP functions
  • Detailed explanation of the usage of wp_title() function in WordPress development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089578.htmlTechArticleUsage analysis of related functions for title display in WordPress development, wordpress title single_cat_title() function single_cat_title() function, We rarely use it in daily life, but this function...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template