国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress開發(fā)中用于標(biāo)題顯示的相關(guān)函數(shù)使用解析

2024-09-07 00:48:04
字體:
供稿:網(wǎng)友

這篇文章主要介紹了WordPress開發(fā)中用于標(biāo)題顯示的相關(guān)函數(shù)使用解析,講解了single_cat_title函數(shù)和get_the_title函數(shù)和the_title函數(shù)的用法,需要的朋友可以參考下

single_cat_title()函數(shù)

single_cat_title()函數(shù),日常中我們很少會用到,但這個函數(shù)會給我們解決很多問題,諸如當(dāng)前頁面的目錄、標(biāo)簽,該函數(shù)不依附于 WordPress 主循環(huán)中,也不能放入主循環(huán)中使用。

描述

獲取當(dāng)前頁面的分類、標(biāo)簽。

<?php single_cat_title($prefix,$display); ?>

$prefix :用于設(shè)置在標(biāo)題之前顯示的內(nèi)容。

$display :用于設(shè)置是直接顯示還是返回到變量。

實例

在此摘取 WordPress 2011 默認(rèn)主題中,category.php 文件 第18行左右位置的代碼

  1. <?php 
  2. printf( __( 'Category Archives: %s''twentyeleven' ), '<span>' . single_cat_title( '', false ) . '</span>' ); 
  3. ?> 

get_the_title 和 the_title

get_the_title 和 the_title 兩個函數(shù)用來在文章頁面顯示文章標(biāo)題的函數(shù),之所以將兩個函數(shù)合并到一篇文章里面去是因為這兩個函是一個實現(xiàn),只不過 the_title 默認(rèn)直接顯示,get_the_title 默認(rèn)返回字符串,如果你對此心存疑惑,那請你往下看。

函數(shù)詳解

get_the_title 和 the_title這兩個函數(shù)主要用于在循環(huán)中顯示當(dāng)前文章的標(biāo)題,請注意 the_title 這個函數(shù)必須使用在循環(huán)中。

兩者的區(qū)別在于,get_the_title僅能以字符串形式返回文章標(biāo)題,而 the_title 可以設(shè)置標(biāo)題前后的自定義字符,以及是顯示還是返回字符串。

the_title 函數(shù)使用、參數(shù)詳解

<?php the_title( $before, $after, $echo ); ?>

$before標(biāo)題前的字符

$after標(biāo)題后的字符

$echo顯示、還是返回字符串,默認(rèn)為true

the_title示例

<?php the_title( ‘=>', ‘<=' ); ?>

以本文為例,我們將得到以下這樣的標(biāo)題:

‘=>get_the_title 和 the_title<='

get_the_title 函數(shù)使用、參數(shù)詳解

<?php $myTitle = get_the_title($ID); ?>

以上代碼我們將得到文章標(biāo)題的變量$myTitle;

$ID 用于設(shè)置文章 ID ,當(dāng)然在循環(huán)中我們可以省略此參數(shù)。

get_the_title 示例

  1. <?php 
  2.  $myTitle = get_the_title($ID);  
  3.  echo $mytitle.'【標(biāo)題演示】'
  4. ?> 

我們將得到

get_the_title 和 the_title【標(biāo)題演示】

總結(jié)

說了這么多,不知道對您是否有所幫助?

總的來說 the_title 是 get_the_title的更高一級封裝。就像在 wp_title中說的那樣,更高級封裝,雖然使用起來簡單,但能折騰花樣相對少了點。

下面是該兩個函數(shù)的源代碼

the_title 函數(shù)聲明

該函數(shù)位于 wp-include/post-template.php 文件的 43 – 55行左右的位置

  1. <?php 
  2. /** 
  3.  * Display or retrieve the current post title with optional content. 
  4.  * 
  5.  * @since 0.71 
  6.  * 
  7.  * @param string $before Optional. Content to prepend to the title. 
  8.  * @param string $after Optional. Content to append to the title. 
  9.  * @param bool $echo Optional, default to true.Whether to display or return. 
  10.  * @return null|string Null on no title. String if $echo parameter is false. 
  11.  */ 
  12. function the_title($before = ''$after = ''$echo = true) { 
  13.  $title = get_the_title(); 
  14.    
  15.  if ( strlen($title) == 0 ) 
  16.  return
  17.    
  18.  $title = $before . $title . $after
  19.    
  20.  if ( $echo ) 
  21.  echo $title
  22.  else 
  23.  return $title
  24. ?> 

get_the_title 函數(shù)聲明

該函數(shù)位于 wp-include/post-template.php 文件的 103 – 118行左右的位置

  1. <?php 
  2. /** 
  3.  * Retrieve post title. 
  4.  * 
  5.  * If the post is protected and the visitor is not an admin, then "Protected" 
  6.  * will be displayed before the post title. If the post is private, then 
  7.  * "Private" will be located before the post title. 
  8.  * 
  9.  * @since 0.71 
  10.  * 
  11.  * @param int $id Optional. Post ID. 
  12.  * @return string 
  13.  */ 
  14. function get_the_title( $id = 0 ) { 
  15.  $post = &get_post($id); 
  16.    
  17.  $title = isset($post->post_title) ? $post->post_title : ''
  18.  $id = isset($post->ID) ? $post->ID : (int) $id
  19.    
  20.  if ( !is_admin() ) { 
  21.  if ( !emptyempty($post->post_password) ) { 
  22.   $protected_title_format = apply_filters('protected_title_format', __('Protected: %s')); 
  23.   $title = sprintf($protected_title_format$title); 
  24.  } else if ( isset($post->post_status) && 'private' == $post->post_status ) { 
  25.   $private_title_format = apply_filters('private_title_format', __('Private: %s')); 
  26.   $title = sprintf($private_title_format$title); 
  27.  } 
  28.  } 
  29.  return apply_filters( 'the_title'$title$id ); 
  30. ?>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 左云县| 望都县| 兴义市| 长乐市| 嘉祥县| 观塘区| 秦皇岛市| 三明市| 时尚| 肥东县| 青州市| 台前县| 双峰县| 友谊县| 寿光市| 宁陕县| 桃江县| 阳城县| 象州县| 萝北县| 正阳县| 永安市| 西青区| 三都| 上虞市| 高碑店市| 南通市| 观塘区| 涟水县| 广汉市| 驻马店市| 永兴县| 团风县| 石城县| 诸暨市| 竹山县| 泉州市| 民丰县| 江川县| 盖州市| 三门峡市|