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数据备份心得分享

效劳器钱用光了要关了或许是效劳器想要搬家,需求备份各种数据。 明天简略的备份了一下在效劳器下面wordpre...

Google公布民间WordPress插件:Google Publisher Plugin

民间引见: 1,经过插件能够创立adsense广告位。 2,能够间接经过工具在网管中心验证网站。 之前站长David...

WordPress 增加Meta Box的办法

置信很多站长都晓得并且曾经在应用WordPress自定义字段,很多插件也应用了这一性能。 自定义字段是一个十分...

WordPress完成加入登录后跳转到指定页面的办法

本文实例讲述了WordPress完成加入登录后跳转到指定页面的办法。分享给大家供大家参考。详细办法如下: 有时...

wordpress get_posts函数的应用办法 制止输入指定类别的文章

应用wordpress制止输入指定类别的文章能够给get_posts()函数传个数组参数,如下:复制代码代码如下:<p clas...

需求提交

客服服务