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 图片用独自域名贮存办法

之前我都是用二级域名的方式来存储图片、JS 和 CSS ,前些天在推特上的一位顺序员 yoursunny 通知我,光用二...

Wordpress 处理注册登录死循环成绩

【成绩】:网站(Wordpress)注册登录死循环 【症状】:在公司里,局部网站(包括wordpress)登录注册时,提...

在SAE上搭建最新wordpress的办法

装置SAE上的wordpress,创立使用抉择wordpress模板,装置后是3.4版本 新建一个版本2,下载最新wordpress装置...

如何在wordpress上添加文件上传的大小限度(多种完成办法)

本文只限Apache配置主机的应用办法,添加WordPress文件上传大小限度:经过配置php.ini的upload_max_filesiz...

详解WordPress中创立和增加过滤器的相干PHP函数

apply_filters()(创立过滤器) apply_filters() 函数用来创立一个过滤器,大少数被用在函数中,是 WordPre...

WordPress初级自定义规划的内容编辑器(TinyMCE)模板

WordPress的编辑器TinyMCE是一个十分弱小的工具,关于网页设计师来说,应用WordPress的编辑器TinyMCE是没什...

需求提交

客服服务