微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WordPress开发中用于标题显示的相关函数使用解析

single_cat_title()函数

single_cat_title()函数,日常中我们很少会用到,但这个函数会给我们解决很多问题,诸如当前页面的目录、标签,该函数不依附于 wordpress 主循环中,也不能放入主循环中使用。

描述 获取当前页面分类标签

rush:PHP;">

实例 在此摘取 wordpress 2011 主题中,category.PHP 文件 第18行左右位置的代码

rush:PHP;"> PHP printf( __( 'Category Archives: %s','twentyeleven' ),'' . single_cat_title( '',false ) . '' ); ?>

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 函数使用、参数详解

rush:PHP;">

the_title示例

rush:PHP;"> ',‘<=' ); ?>

以本文为例,我们将得到以下这样的标题

get_the_title 和 the_title<='

get_the_title 函数使用、参数详解

rush:PHP;">

以上代码我们将得到文章标题的变量$myTitle; $ID 用于设置文章 ID ,当然在循环中我们可以省略此参数。

get_the_title 示例

rush:PHP;">

我们将得到

get_the_title 和 the_title【标题演示】

总结 说了这么多,不知道对您是否有所帮助? 总的来说 the_title 是 get_the_title的更高一级封装。就像在 wp_title中说的那样,更高级封装,虽然使用起来简单,但能折腾花样相对少了点。 下面是该两个函数的源代码

the_title 函数声明 该函数位于 wp-include/post-template.PHP 文件的 43 – 55行左右的位置

rush:PHP;"> 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行左右的位置

rush:PHP;"> $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 );
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐