WordPress开发中用于标题显示的相关函数使用解析_PHP
May 28, 2016 am 11:49 AMsingle_cat_title()函数
single_cat_title()函数,日常中我们很少会用到,但这个函数会给我们解决很多问题,诸如当前页面的目录、标签,该函数不依附于 WordPress 主循环中,也不能放入主循环中使用。
描述
获取当前页面的分类、标签。
1 |
|
- $prefix :用于设置在标题之前显示的内容。
- $display :用于设置是直接显示还是返回到变量。
实例
在此摘取 WordPress 2011 默认主题中,category.php 文件 第18行左右位置的代码
1 2 3 |
|
get_the_title 和 the_title
get_the_title 和 the_title 两个函数用来在文章页面显示文章标题的函数,之所以将两个函数合并到一篇文章里面去是因为这两个函是一个实现,只不过 the_title 默认直接显示,get_the_title 默认返回字符串,如果你对此心存疑惑,那请你往下看。
函数详解
get_the_title 和 the_title这两个函数主要用于在循环中显示当前文章的标题,请注意 the_title 这个函数必须使用在循环中。
两者的区别在于,get_the_title仅能以字符串形式返回文章标题,而 the_title 可以设置标题前后的自定义字符,以及是显示还是返回字符串。
the_title 函数使用、参数详解
1 |
|
- $before标题前的字符
- $after标题后的字符
- $echo显示、还是返回字符串,默认为true
the_title示例
1 |
|
以本文为例,我们将得到以下这样的标题:
1 |
|
get_the_title 函数使用、参数详解
1 |
|
以上代码我们将得到文章标题的变量$myTitle;
$ID 用于设置文章 ID ,当然在循环中我们可以省略此参数。
get_the_title 示例
1 2 3 4 |
|
我们将得到
get_the_title 和 the_title【标题演示】
总结
说了这么多,不知道对您是否有所帮助?
总的来说 the_title 是 get_the_title的更高一级封装。就像在 wp_title中说的那样,更高级封装,虽然使用起来简单,但能折腾花样相对少了点。
下面是该两个函数的源代码
the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 43 – 55行左右的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
get_the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 103 – 118行左右的位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|

Article chaud

Outils chauds Tags

Article chaud

Tags d'article chaud

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

PHP vs Flutter : le meilleur choix pour le développement mobile

Comment changer la largeur d'une page dans WordPress

Dans quel dossier se trouvent les articles wordpress ?

Comment créer une page produit dans WordPress

Où se trouve le fichier de modèle WordPress ?

Comment rechercher des auteurs dans WordPress

Quelle version de wordpress est stable ?

Quel langage est utilisé pour développer WordPress ?
