麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > 建站經驗 > 正文

WordPress開發中用于標題顯示的相關函數使用解析

2024-04-25 20:43:52
字體:
來源:轉載
供稿:網友

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 );

}

?>

以上就是本文章的內容,希望對大家有所幫助

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中文字幕在线播放第一页 | 99精品欧美一区二区 | 久久精热 | 最新一区二区三区 | 日本在线播放一区二区三区 | 国产一区二区成人在线 | h视频在线播放 | 久久久久久久久免费 | 亚欧在线免费观看 | 精品一区二区三区免费 | 国产精品免费视频观看 | 欧美wwwsss9999| 成年性羞羞视频免费观看 | 欧美日本91精品久久久久 | 日韩99 | 中文字幕 亚洲一区 | 国产一区视频在线免费观看 | 91亚洲精品一区二区福利 | 最新一区二区三区 | h网站在线观看 | 欧美日日操 | 免费在线观看亚洲 | 高清国产在线 | 中文字幕在线资源 | 久久蜜桃精品一区二区三区综合网 | 成人片免费视频 | lutube成人福利在线观看污 | 国产无遮挡一区二区三区毛片日本 | 蜜桃视频日韩 | xxx日本视频| 综合毛片 | 欧美国产精品久久 | 99精彩视频在线观看 | 美国av免费看 | 黄色网址在线视频 | 99精品视频在线免费观看 | 精品一区二区三区免费看 | 久久国产精品91 | 在线免费观看毛片 | 黄色片网站免费在线观看 | 中国老女人一级毛片视频 |