《PHP學(xué)習(xí):WordPress開發(fā)中用于標(biāo)題顯示的相關(guān)函數(shù)使用解析》要點(diǎn):
本文介紹了PHP學(xué)習(xí):WordPress開發(fā)中用于標(biāo)題顯示的相關(guān)函數(shù)使用解析,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
single_cat_title()函數(shù)
single_cat_title()函數(shù),日常中我們很少會(huì)用到,但這個(gè)函數(shù)會(huì)給我們辦理很多問題,諸如當(dāng)前頁面的目錄、標(biāo)簽,該函數(shù)不依附于 WordPress 主循環(huán)中,也不能放入主循環(huán)中使用.PHP實(shí)例
描寫
獲取當(dāng)前頁面的分類、標(biāo)簽.PHP實(shí)例
<?php single_cat_title($prefix,$display); ?>
實(shí)例
在此摘取 WordPress 2011 默認(rèn)主題中,category.php 文件 第18行左右位置的代碼PHP實(shí)例
<?php printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' ); ?>
get_the_title 和 the_title
get_the_title 和 the_title 兩個(gè)函數(shù)用來在文章頁面顯示文章題目的函數(shù),之所以將兩個(gè)函數(shù)合并到一篇文章里面去是因?yàn)檫@兩個(gè)函是一個(gè)實(shí)現(xiàn),只不過 the_title 默認(rèn)直接顯示,get_the_title 默認(rèn)返回字符串,如果你對(duì)此心存疑惑,那請(qǐng)你往下看.PHP實(shí)例
函數(shù)詳解
get_the_title 和 the_title這兩個(gè)函數(shù)主要用于在循環(huán)中顯示當(dāng)前文章的標(biāo)題,請(qǐng)注意 the_title 這個(gè)函數(shù)必需使用在循環(huán)中.
兩者的區(qū)別在于,get_the_title僅能以字符串形式返回文章標(biāo)題,而 the_title 可以設(shè)置標(biāo)題前后的自定義字符,以及是顯示還是返回字符串.PHP實(shí)例
the_title 函數(shù)使用、參數(shù)詳解
PHP實(shí)例
<?php the_title( $before, $after, $echo ); ?>
the_title示例PHP實(shí)例
<?php the_title( ‘=>', ‘<=' ); ?>
以本文為例,我們將得到以下這樣的題目:PHP實(shí)例
‘=>get_the_title 和 the_title<='
get_the_title 函數(shù)使用、參數(shù)詳解
PHP實(shí)例
<?php $myTitle = get_the_title($ID); ?>
以上代碼我們將得到文章題目的變量$myTitle;
$ID 用于設(shè)置文章 ID ,當(dāng)然在循環(huán)中我們可以省略此參數(shù).PHP實(shí)例
get_the_title 示例
PHP實(shí)例
<?php $myTitle = get_the_title($ID); echo $mytitle.'【題目演示】'; ?>
我們將得到PHP實(shí)例
get_the_title 和 the_title【題目演示】PHP實(shí)例
總結(jié)
說了這么多,不知道對(duì)您是否有所贊助?
總的來說 the_title 是 get_the_title的更高一級(jí)封裝.就像在 wp_title中說的那樣,更高級(jí)封裝,雖然使用起來簡單,但能折騰花樣相對(duì)少了點(diǎn).
下面是該兩個(gè)函數(shù)的源代碼PHP實(shí)例
the_title 函數(shù)聲明
該函數(shù)位于 wp-include/post-template.php 文件的 43 C 55行左右的地位PHP實(shí)例
<?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 函數(shù)聲明
該函數(shù)位于 wp-include/post-template.php 文件的 103 C 118行左右的地位PHP實(shí)例
<必修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 ); } 必修>
維易PHP培訓(xùn)學(xué)院每天發(fā)布《PHP學(xué)習(xí):WordPress開發(fā)中用于標(biāo)題顯示的相關(guān)函數(shù)使用解析》等實(shí)戰(zhàn)技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養(yǎng)人才。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7783.html