Home>

single_cat_title () functionsingle_cat_title () function, we rarely use it 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,It cannot be used in the main loop.

description

Get 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 display directly or return to a variable.

Examples

In this extract of the wordpress 2011 default theme,category.php file Code around line 18

<?php
printf (__ ("category archives:%s", "twentyeleven"), "<span>". single_cat_title ("", false). "</span>");
?>

get_the_title and the_titleThe functions get_the_title and the_title are used to display the title of the article on the article page.The reason why the two functions are merged into one article is because these two functions are an implementation,It's just that the_title is displayed directly by default,get_the_title returns a string by default,If you are confused about this,Then please look down.

Detailed function

The two functions get_the_title and the_title are mainly used to display the title of the current article in a loop.Note that the_title function must be used in a loop.

The difference between the two is thatget_the_title can only return the title of the article as a string.The the_title can set custom characters before and after the title,And whether to display or return a string.

The_title function use, detailed parameters

?php the_title ($before, $after, $echo);?>
$before the character before the title $after character after title $echo shows or returns a string,Default is true

the_title example

<?php the_title ("=>", "<=");?>

Take this article as an example,We will get the following titles:

"=&get;get_the_title and the_title<="

Get_the_title function use, detailed parameters

<?php $mytitle=get_the_title ($id);?>

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. "[title presentation]";
?>

We will get

get_the_title and the_title [title demo]

to sum up

In general the_title is a higher-level wrapper for get_the_title.As said in wp_title,More advanced packaging,Although simple to use,But there are relatively few tricks.

Here 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);
}
?>
php
  • Previous Notification notification usage examples (vibration, light, sound) in Android
  • Next Addcslashes and stripcslashes function usage analysis in PHP