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.
<?php single_cat_title($prefix,$display); ?>
Example
Here is an excerpt of the code around line 18 of the category.php file in the WordPress 2011 default theme
<?php printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?>
get_the_title and the_title
get_the_title and the_title are two functions used to display the article title on the article page. The reason why the two functions are merged into an 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
<?php the_title( $before, $after, $echo ); ?>
the_title example
<?php the_title( ‘=>', ‘<=' ); ?>
Take this article as an example, we will get the following title:
‘=>get_the_title 和 the_title<='
get_the_title function usage and detailed explanation of parameters
<?php $myTitle = get_the_title($ID); ?>
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
<?php $myTitle = get_the_title($ID); echo $mytitle.'【标题演示】'; ?>
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
<?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 around lines 103 – 118 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 ); } ?>