加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 03:04:26 所属栏目:PHP教程 来源:网络整理
导读:《:WordPress开发中用于标题显示的相关函数使用解析》要点: 本文介绍了:WordPress开发中用于标题显示的相关函数使用解析,希望对您有用。如果有疑问,可以联系我们。 single_cat_title()函数 single_cat_title()函数,日常中我们很少会用到,但这个函数会给

《:WordPress开发中用于标题显示的相关函数使用解析》要点:
本文介绍了:WordPress开发中用于标题显示的相关函数使用解析,希望对您有用。如果有疑问,可以联系我们。

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

描写
获取当前页面的分类、标签.PHP实例

 <?php single_cat_title($prefix,$display); ?>
  • $prefix :用于设置在题目之前显示的内容.
  • $display :用于设置是直接显示还是返回到变量.

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

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

get_the_title 和 the_title
get_the_title 和 the_title 两个函数用来在文章页面显示文章题目的函数,之所以将两个函数合并到一篇文章里面去是因为这两个函是一个实现,只不过 the_title 默认直接显示,get_the_title 默认返回字符串,如果你对此心存疑惑,那请你往下看.PHP实例

函数详解
get_the_title 和 the_title这两个函数主要用于在循环中显示当前文章的标题,请注意 the_title 这个函数必需使用在循环中.
两者的区别在于,get_the_title仅能以字符串形式返回文章标题,而 the_title 可以设置标题前后的自定义字符,以及是显示还是返回字符串.PHP实例

the_title 函数使用、参数详解
PHP实例

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

the_title示例PHP实例

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

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

‘=>get_the_title 和 the_title<='

get_the_title 函数使用、参数详解
PHP实例

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

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

get_the_title 示例
PHP实例

<?php
 $myTitle = get_the_title($ID); 
 echo $mytitle.'【题目演示】';
?>

我们将得到PHP实例

get_the_title 和 the_title【题目演示】PHP实例

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

the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 43 C 55行左右的地位PHP实例

<?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 函数声明
该函数位于 wp-include/post-template.php 文件的 103 C 118行左右的地位PHP实例

<必修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 );
}
必修>

编程之家培训学院每天发布《:WordPress开发中用于标题显示的相关函数使用解析》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读