recherche
Maisondéveloppement back-endtutoriel phpWordPress开发中用于标题显示的相关函数使用解析,wordpress标题_PHP教程

WordPress开发中用于标题显示的相关函数使用解析,wordpress标题

single_cat_title()函数
single_cat_title()函数,日常中我们很少会用到,但这个函数会给我们解决很多问题,诸如当前页面的目录、标签,该函数不依附于 WordPress 主循环中,也不能放入主循环中使用。

描述
获取当前页面的分类、标签。

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

  • $prefix :用于设置在标题之前显示的内容。
  • $display :用于设置是直接显示还是返回到变量。

实例
在此摘取 WordPress 2011 默认主题中,category.php 文件 第18行左右位置的代码

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

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 函数使用、参数详解

<&#63;php the_title( $before, $after, $echo ); &#63;>
  • $before标题前的字符
  • $after标题后的字符
  • $echo显示、还是返回字符串,默认为true

the_title示例

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

以本文为例,我们将得到以下这样的标题:

‘=>get_the_title 和 the_title<='

get_the_title 函数使用、参数详解

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

以上代码我们将得到文章标题的变量$myTitle;
$ID 用于设置文章 ID ,当然在循环中我们可以省略此参数。

get_the_title 示例

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

我们将得到

get_the_title 和 the_title【标题演示】

总结
说了这么多,不知道对您是否有所帮助?
总的来说 the_title 是 get_the_title的更高一级封装。就像在 wp_title中说的那样,更高级封装,虽然使用起来简单,但能折腾花样相对少了点。
下面是该两个函数的源代码

the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 43 – 55行左右的位置

<&#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;>

get_the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 103 – 118行左右的位置

<&#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;>

您可能感兴趣的文章:

  • WordPress中调试缩略图的相关PHP函数使用解析
  • 配置解决Nginx服务器中WordPress路径不自动加斜杠问题
  • WordPress中用于获取搜索表单的PHP函数使用解析
  • 在WordPress中使用wp_count_posts函数来统计文章数量
  • 详解WordPress中调用评论模板和循环输出评论的PHP函数
  • 详解WordPress中分类函数wp_list_categories的使用
  • WordPress中限制非管理员用户在文章后只能评论一次
  • 详解WordPress中创建和添加过滤器的相关PHP函数
  • 详解WordPress开发中wp_title()函数的用法

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1089578.htmlTechArticleWordPress开发中用于标题显示的相关函数使用解析,wordpress标题 single_cat_title()函数 single_cat_title()函数,日常中我们很少会用到,但这个函数...
Déclaration
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
PHP Dependency Injection Container: un démarrage rapidePHP Dependency Injection Container: un démarrage rapideMay 13, 2025 am 12:11 AM

AphpdependencyInjectionContaineeRisatool That ManageSclassDensences, améliorant le codemodularité, la testabilité et la participation.

Injection de dépendance vs localisateur de service en PHPInjection de dépendance vs localisateur de service en PHPMay 13, 2025 am 12:10 AM

Sélectionnez DependencyInjection (DI) Pour les grandes applications, le service de service convient aux petits projets ou prototypes. 1) DI améliore la testabilité et la modularité du code par injection de constructeur. 2) Servicelocator obtient des services par l'enregistrement du centre, ce qui est pratique mais peut entraîner une augmentation du couplage du code.

Stratégies d'optimisation des performances PHP.Stratégies d'optimisation des performances PHP.May 13, 2025 am 12:06 AM

PhpapplicationsCanBeoptimizedForsPeedAndFiciency: 1) AutoringOpCacheInphp.ini, 2) Utilisation de PreparedStatementswithpodOrdatabasequeries, 3) Remplacingloopswitray_filterandArray_Mapfordataprocessing, 4) Configurationnginxasareproxy, 5)

Validation des e-mails PHP: garantir que les e-mails sont envoyés correctementValidation des e-mails PHP: garantir que les e-mails sont envoyés correctementMay 13, 2025 am 12:06 AM

PhpemailvalidationInvolvesthestreps: 1) formatvalidationusinggularexpressionstochecktheemailformat; 2) dnsvalidationtoensethedomainhasavalidmxrecord; 3) smtpvalidation, themostthoroughMethod.

Comment faire des applications PHP plus rapidementComment faire des applications PHP plus rapidementMay 12, 2025 am 12:12 AM

TomakePhpApplicationsFaster, suivez lesEsthets: 1) useopCodeCachingLikeOpcachetOsToreprecCompileScriptByteCode.2) MinimizedatabaseQueriesByusingSingQueryCachingAndefficientIndexing.3) Levier

Liste de contrôle d'optimisation des performances PHP: améliorez la vitesse maintenantListe de contrôle d'optimisation des performances PHP: améliorez la vitesse maintenantMay 12, 2025 am 12:07 AM

TOIMPROVEPHPAPPLICATIONSPEED, SuivreSesEtsts: 1) Activer le codécachement de la codécaching avec un accumulation de dotation

Injection de dépendance PHP: améliorer la testabilité du codeInjection de dépendance PHP: améliorer la testabilité du codeMay 12, 2025 am 12:03 AM

L'injection de dépendance (DI) améliore considérablement la testabilité du code PHP par des dépendances explicitement transitives. 1) Les classes de découplage en DI et les implémentations spécifiques rendent les tests et la maintenance plus flexibles. 2) Parmi les trois types, le constructeur injecte des dépendances d'expression explicites pour maintenir l'état cohérent. 3) Utilisez des conteneurs DI pour gérer les dépendances complexes pour améliorer la qualité du code et l'efficacité de développement.

Optimisation des performances PHP: optimisation des requêtes de base de donnéesOptimisation des performances PHP: optimisation des requêtes de base de donnéesMay 12, 2025 am 12:02 AM

DatabaseQueryOptimizationInphpinvolveSeveralStrategieSoenhancePerformance.1) selectOnlyneceSaryColumnStoredatatransfer.2) useIndexingTospeeDupDatareTeval.3) ImplémentQueryCachingTostoreResul

See all articles

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

Video Face Swap

Video Face Swap

Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Article chaud

<🎜>: Bubble Gum Simulator Infinity - Comment obtenir et utiliser les clés royales
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers of the Witch Tree - Comment déverrouiller le grappin
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
Nordhold: Système de fusion, expliqué
3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌

Outils chauds

Version crackée d'EditPlus en chinois

Version crackée d'EditPlus en chinois

Petite taille, coloration syntaxique, ne prend pas en charge la fonction d'invite de code

PhpStorm version Mac

PhpStorm version Mac

Le dernier (2018.2.1) outil de développement intégré PHP professionnel

SublimeText3 Linux nouvelle version

SublimeText3 Linux nouvelle version

Dernière version de SublimeText3 Linux

Version Mac de WebStorm

Version Mac de WebStorm

Outils de développement JavaScript utiles

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Puissant environnement de développement intégré PHP