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 评论者超链接完成重定向跳转的办法

渣滓评论曾经不是一个新颖的货色了,置信只需是日独立IP超越2000的网站,只需开放评论的话,每天都会收到几...

WordPress博客零碎搜寻引擎优化seo全攻略

1.空间的抉择 wordpress要求 php+mysql的支持,rewrite性能,比方apache的mod-rewrite或iis rewrite。 空间...

利用Fix Rss Feeds插件修复WordPress的Feed显示谬误

明天忽然有个网友留言说博客的Feed挂了,症状如下: Chrome间接关上订阅页面显示如下: This page contain...

wordpress上传图片不主动生成缩略图的处理办法

由于站点不需求显示这么多图片,只要要原图就OK了,所以就要修正下媒体设置。关上设置->媒体,将不需求的缩...

如何进行Wordpress的后盾设置

要害字形容:设置 后盾 进行 如何 这里 Wordpress 选项 能够 网站 你曾经阅读了这个 Wordpress 架设的站点...

wordpress设置友谊链接只在首页显示的办法

关于有的主题,当从后盾小工具中增加友谊链接后,却发现其是为全站显示的。假如只心愿让它在首页显示,则能...

需求提交

客服服务