wp文本小工具運行php代碼實現麵包屑導航步驟

來源:互聯網
上載者:User
在wordpress中,很多主題都提供了很多小工具,其中很有用的一個小工具就是文本小工具。

下面說一下具體如何讓文本小工具運行php代碼來實現麵包屑導航。

1.首先在主題下的function.php中要先添加麵包屑導航函數

//添加麵包屑導航function iaccepted_breadcrumbs() {  $delimiter = '»';  $name = 'Home'; //text for the 'Home' link  $currentBefore = '';  $currentAfter = '';  if ( !is_home() && !is_front_page() || is_paged() ) {    echo '';    global $post;    $home = get_bloginfo('url');    echo '' . $name . ' ' . $delimiter . ' ';    if ( is_category() ) {      global $wp_query;      $cat_obj = $wp_query->get_queried_object();      $thisCat = $cat_obj->term_id;      $thisCat = get_category($thisCat);      $parentCat = get_category($thisCat->parent);      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));      echo $currentBefore;      single_cat_title();      echo $currentAfter;    } elseif ( is_day() ) {      echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';      echo '' . get_the_time('F') . ' ' . $delimiter . ' ';      echo $currentBefore . get_the_time('d') . $currentAfter;    } elseif ( is_month() ) {      echo '' . get_the_time('Y') . ' ' . $delimiter . ' ';      echo $currentBefore . get_the_time('F') . $currentAfter;    } elseif ( is_year() ) {      echo $currentBefore . get_the_time('Y') . $currentAfter;    } elseif ( is_single() ) {      $cat = get_the_category(); $cat = $cat[0];      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');      echo $currentBefore;      the_title();      echo $currentAfter;    } elseif ( is_page() && !$post->post_parent ) {      echo $currentBefore;      the_title();      echo $currentAfter;    } elseif ( is_page() && $post->post_parent ) {      $parent_id  = $post->post_parent;      $breadcrumbs = array();      while ($parent_id) {        $page = get_page($parent_id);        $breadcrumbs[] = 'ID) . '">' . get_the_title($page->ID) . '';        $parent_id  = $page->post_parent;      }      $breadcrumbs = array_reverse($breadcrumbs);      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';      echo $currentBefore;      the_title();      echo $currentAfter;    } elseif ( is_search() ) {      echo $currentBefore . get_search_query() . $currentAfter;    } elseif ( is_tag() ) {      echo $currentBefore;      single_tag_title();      echo $currentAfter;    } elseif ( is_author() ) {       global $author;      $userdata = get_userdata($author);      echo $currentBefore .' ' . $userdata->display_name . $currentAfter;    } elseif ( is_404() ) {      echo $currentBefore . 'Error 404' . $currentAfter;    }    if ( get_query_var('paged') ) {      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';      echo __('Page') . ' ' . get_query_var('paged');      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';    }    echo '';  }}

2要讓小工具能夠運行php代碼必須要現在主題下的functions.php中加入如下代碼

//讓小工具中可以嵌入php代碼function php_text($text){if (strpos($text, '<'.'?')!==false){ob_start();eval('?'.'>'.$text);$text = ob_get_contents();ob_end_clean();}return $text;}add_filter('widget_text', 'php_text', 90);

這個樣就能保證文本小工具中可以嵌入php代碼並能正常執行。

3.選擇要加入導航的位置,放置一個文本小工具,然後標題不需要填,內容填寫如下:

您現在的位置:

這樣就完成了在文本小工具中運行php代碼實現麵包屑導航,具體效果可以看本文章上方的導航。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.