WordPress开发中用于题目显示的相干函数应用解析

single_cat_title()函数
single_cat_title()函数,日常中咱们很少会用到,但这个函数会给咱们处理很多成绩,诸如以后页面的目录、标签,该函数不附丽于 WordPress 主循环中,也不能放入主循环中应用。

形容
获取以后页面的分类、标签。

 <?php single_cat_title($prefix,$display); ?>

  • $prefix :用于设置在题目之前显示的内容。
  • $display :用于设置是间接显示还是前往到变量。

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

 <?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 默许前往字符串,假如你对此心存纳闷,那请你往下看。

函数详解
get_the_title 和 the_title这两个函数次要用于在循环中显示以后文章的题目,请留意 the_title 这个函数必需应用在循环中。
两者的区别在于,get_the_title仅能以字符串方式前往文章题目,而 the_title 能够设置题目前后的自定义字符,以及是显示还是前往字符串。

the_title 函数应用、参数详解

<?php the_title( $before, $after, $echo ); ?>

  • $before题目前的字符
  • $after题目后的字符
  • $echo显示、还是前往字符串,默许为true

the_title示例

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

以本文为例,咱们将失去以下这样的题目:

‘=>get_the_title 和 the_title<='

get_the_title 函数应用、参数详解

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

以上代码咱们将失去文章题目的变量$myTitle;
$ID 用于设置文章 ID ,当然在循环中咱们能够省略此参数。

get_the_title 示例

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

咱们将失去

get_the_title 和 the_title【题目演示】

总结
说了这么多,不晓得对您能否有所协助?
总的来说 the_title 是 get_the_title的更高一级封装。就像在 wp_title中说的那样,更初级封装,尽管应用起来简略,但能折腾把戏**少了点。
上面是该两个函数的源代码

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

<?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 – 118行左右的地位

<?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开发中用于标题显示的相关函数使用解析》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress 标题 PHP编程
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress完成搜寻后果只有一篇文章时主动跳转到文章的办法

本文实例讲述了WordPress完成搜寻后果只有一篇文章时主动跳转到文章的办法。分享给大家供大家参考。详细剖析...

WordPress主动给文章增加nofollow属性的完成办法

本文实例讲述了WordPress主动给文章增加nofollow属性的完成办法。分享给大家供大家参考。详细剖析如下: no...

完满处理wordpress sae版改域名后不能进入后盾的成绩

处理方法: 办法一: 修正文件&ldquo;/wp-includes/pluggable.php&rdquo;中的wp_set_auth_cookie函数。搜寻...

wordpress晋级后明码正确后盾无奈登陆的处理办法

本文实例讲述了wordpress晋级后明码正确后盾无奈登陆的处理办法。分享给大家供大家参考。详细剖析如下: 明...

如何将WordPress博客订阅到QQ邮箱详细该怎样操作

1、登录QQ邮箱,点击右下角&ldquo;浏览空间&rdquo;,进入浏览空间。 2、进入浏览空间后,点击页面右上角&ld...

WordPress免登录公布接口,支持一切wordpress版本

申明2:请尊重作者休息,谴责拷贝后免费的行为。 公布不胜利的请留意反省几点:1. 公布的编码留意应该是utf...

需求提交

客服服务