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无插件调用最新、抢手、随机文章实例代码

调用最新文章: 复制代码代码如下: <ul> <?php $post_query = new WP_Query(&lsquo;showposts=10&prime;); ...

wordpress应用外链图片作为文章缩略图的办法

1、要有一个确定图片地址的办法:文章中的第一张图片,或许应用自定义栏目添加一个自定义值。2、在前台调用...

wordpress主题中style.css文件需求用到的正文阐明整顿

复制代码代码如下: /* Theme Name: 这里填主题称号 Theme URI: 这里填主题引见的网址,没有就填你的博客网址...

WordPress中"无奈将上传的文件挪动至"谬误的处理办法

明天在网页上传图片到博客,后果提醒:“无奈将上传的文件挪动至 /home/wwwroot/wp-content/uploads/2013/”...

wordpress动态化首页及去除url中的index.html

应用wordpass博客顺序的冤家都晓得他的默许首页是index.php,但是地球人都晓得index.html的太首页可以进步网...

wordpress模板页面判别函数(本人整顿)

本人平常用到的一些函数,整顿一下,大家有用到来拿吧,我怕放电脑上不见了,当前又得找。 Index page 首页...

需求提交

客服服务