首頁 後端開發 php教程 WordPress开发中用于标题显示的相关函数使用解析_PHP

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

May 28, 2016 am 11:49 AM
wordpress 標題

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

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

 <&#63;php single_cat_title($prefix,$display); &#63;>

登入後複製

  • $prefix :用于设置在标题之前显示的内容。
  • $display :用于设置是直接显示还是返回到变量。

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

 <&#63;php
printf( __( 'Category Archives: %s', 'twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' );
&#63;>

登入後複製

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

<&#63;php the_title( $before, $after, $echo ); &#63;>
登入後複製

  • $before标题前的字符
  • $after标题后的字符
  • $echo显示、还是返回字符串,默认为true

the_title示例

<&#63;php the_title( ‘=>', ‘<=' ); &#63;>
登入後複製

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

‘=>get_the_title 和 the_title<='

登入後複製

get_the_title 函数使用、参数详解

<&#63;php $myTitle = get_the_title($ID); &#63;>
登入後複製

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

get_the_title 示例

<&#63;php
 $myTitle = get_the_title($ID); 
 echo $mytitle.'【标题演示】';
&#63;>
登入後複製

我们将得到

get_the_title 和 the_title【标题演示】

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

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

<&#63;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;
}
&#63;>
登入後複製

get_the_title 函数声明
该函数位于 wp-include/post-template.php 文件的 103 – 118行左右的位置

<&#63;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) &#63; $post->post_title : '';
 $id = isset($post->ID) &#63; $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 );
}
&#63;>
登入後複製

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP 與 Flutter 的比較:行動裝置開發的最佳選擇 PHP 與 Flutter 的比較:行動裝置開發的最佳選擇 May 06, 2024 pm 10:45 PM

PHP和Flutter是行動端開發的流行技術。 Flutter勝在跨平台能力、效能和使用者介面,適合需要高效能、跨平台和自訂UI的應用程式。 PHP則適用於效能較低、不跨平台的伺服器端應用程式。

wordpress如何修改頁面寬度 wordpress如何修改頁面寬度 Apr 16, 2024 am 01:03 AM

透過編輯 style.css 文件,您可以輕鬆修改 WordPress 頁面寬度:編輯 style.css 文件,新增 .site-content { max-width: [您的首選寬度]; }。修改 [您的首選寬度] 以設定頁面寬度。儲存變更並清除快取(可選)。

wordpress如何做產品頁 wordpress如何做產品頁 Apr 16, 2024 am 12:39 AM

在WordPress 中建立產品頁面:1. 建立產品(名稱、描述、圖片);2. 自訂頁面範本(新增標題、描述、圖片、按鈕);3. 輸入產品資訊(庫存、尺寸、重量);4 .建立變體(不同顏色、尺寸);5. 設定可見性(公開或隱藏);6. 啟用/停用評論;7.預覽並發布頁面。

wordpress文章在哪個資料夾 wordpress文章在哪個資料夾 Apr 16, 2024 am 10:29 AM

WordPress 文章儲存在 /wp-content/uploads 資料夾中。此資料夾使用子資料夾對不同類型的上傳進行分類,包括按年、月和文章 ID 組織的文章。文章檔案以純文字格式 (.txt) 存儲,檔案名稱通常包含其 ID 和標題。

wordpress模板檔案在哪 wordpress模板檔案在哪 Apr 16, 2024 am 11:00 AM

WordPress 範本檔案位於 /wp-content/themes/[主題名稱]/ 目錄。它們用於決定網站的外觀和功能,包括頁首(header.php)、頁尾(footer.php)、主模板(index.php)、單篇文章(single.php)、頁(page.php)、檔案(archive.php)、類別(category.php)、標籤(tag.php)、搜尋(search.php)和404 錯誤頁面(404.php)。透過編輯和修改這些文件,可以自訂 WordPress 網站的外

wordpress如何搜尋作者 wordpress如何搜尋作者 Apr 16, 2024 am 01:18 AM

在 WordPress 中搜尋作者:1. 登入管理面板後,導覽至“文章”或“頁面”,使用搜尋欄輸入作者姓名,在“篩選器”中選擇“作者”。 2. 其他技巧:使用通配符擴大搜尋範圍,使用運算子組合條件,或輸入作者 ID 以搜尋文章。

wordpress哪個版本穩定 wordpress哪個版本穩定 Apr 16, 2024 am 10:54 AM

最穩定的 WordPress 版本是最新版本,因為它包含最新的安全性修補程式、增強效能並引入新功能和改進。為了更新到最新版本,請登入 WordPress 儀表板,前往「更新」頁面並點擊「立即更新」。

wordpress用什麼語言開發 wordpress用什麼語言開發 Apr 16, 2024 am 12:03 AM

WordPress 採用 PHP 語言開發,作為其核心程式語言,用於處理資料庫互動、表單處理、動態內容產生和使用者請求。 PHP 被選擇的原因包括跨平台相容性、易於學習、活躍社群以及豐富的函式庫和框架。除了 PHP,WordPress 還使用 HTML、CSS、JavaScript、SQL 等語言來增強其功能。

See all articles