Analysis of functions used for title display in WordPress development

Source: Internet
Author: User
This article describes how to parse the functions used for title display in WordPress development, and explains how to use the single_cat_title function, get_the_title function, and the_title function. For more information, see Single_cat_title () function
The single_cat_title () function is rarely used in daily use, but this function will solve many problems for us, such as the Directory and tag of the current page. This function is not attached to the WordPress main loop, it cannot be used in the main loop.

Description
Obtain the category and tag of the current page.

 <?php single_cat_title($prefix,$display); ?>

  • $ Prefix: used to set the content displayed before the title.
  • $ Display: used to set whether to directly display or return to a variable.

Instance
In this example, extract the code from around 2011 lines of the category. php file in the default WordPress 18th topic.

 <?phpprintf( __( 'Category Archives: %s', 'twentyeleven' ), '' . single_cat_title( '', false ) . '' );?>

Get_the_title and the_title
The get_the_title and the_title functions are used to display the article title functions on the article page. the reason why the two functions are merged into one article is that these two functions are implemented, but the_title is directly displayed by default, get_the_title returns a string by default. if you are confused about this, please read it down.

Function Details
The get_the_title and the_title functions are mainly used to display the title of the current article in a loop. Note that the the_title function must be used in a loop.
The difference between the two lies in that get_the_title can only return the title of the article in the form of a string, while the_title can set the custom characters before and after the title, as well as whether to display or return the string.

The_title function usage and parameter explanation

<?php the_title( $before, $after, $echo ); ?>

  • $ Before character before the title
  • $ After title character
  • $ Echo: whether to display or return strings. the default value is true.

The_title example

<?php the_title( ‘=>', ‘<=' ); ?>

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

'=> Get_the_title and the_title <='

Get_the_title function usage and parameter details

<?php $myTitle = get_the_title($ID); ?>

The above code will get the variable $ myTitle;
$ ID is used to set the article ID. this parameter can be omitted in the loop.

Example of get_the_title

<? Php $ myTitle = get_the_title ($ ID); echo $ mytitle. '[Title Demo]';?>

We will get

Get_the_title and the_title [title demonstration]

Summary
Having said so much, I wonder if it will help you?
In general, the_title is the higher-level encapsulation of get_the_title. As mentioned in wp_title, more advanced encapsulation is easy to use, but it is a little less practical.
The source code of the two functions is as follows:

The_title function declaration
This function is located at around 43-55 rows of the wp-include/post-template.php file

<?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;}?>

Get_the_title function declaration
This function is located at around 103-118 rows of the wp-include/post-template.php file

<?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) ? $post->post_title : ''; $id = isset($post->ID) ? $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 );}?>

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.