wordpress新版本的网站title在哪里修改?

首先搜索全站或者theme中尤其是function.php中搜索title_tag,一般能在找到生成title的函数,该函数一般在/wp-includes/general-template.php中有个函数function wp-get-document-title(){ },一般在这里面设置title.

一、

在/wp-includes/general-template.php

function _wp_render_title_tag() {
   if ( ! current_theme_supports( 'title-tag' ) ) {
      return;
   }

   echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}

二、
在/wp-includes/general-template.php
function wp_get_document_title() {

   /**
    * Filters the document title before it is generated.
    *
    * Passing a non-empty value will short-circuit wp_get_document_title(),
    * returning that value instead.
    *
    * @since 4.4.0
    *
    * @param string $title The document title. Default empty string.
    */
   $title = apply_filters( 'pre_get_document_title', '' );
   if ( ! empty( $title ) ) {
      return $title;
   }

   global $page, $paged;

   $title = array(
      'title' => '',
   );

   // If it's a 404 page, use a "Page not found" title.
   if ( is_404() ) {
      $title['title'] = __( 'Page not found' );

   // If it's a search, use a dynamic search results title.
   } elseif ( is_search() ) {
      /* translators: %s: search phrase */
      $title['title'] = sprintf( __( 'Search Results for &#8220;%s&#8221;' ), get_search_query() );

   // If on the front page, use the site title.
   } elseif ( is_front_page() ) {
      $title['title'] = get_bloginfo( 'name', 'display' );

   // If on a post type archive, use the post type archive title.
   } elseif ( is_post_type_archive() ) {
      $title['title'] = post_type_archive_title( '', false );

   // If on a taxonomy archive, use the term title.
   } elseif ( is_tax() ) {
      $title['title'] = single_term_title( '', false );

   /*
    * If we're on the blog page that is not the homepage or
    * a single post of any post type, use the post title.
    */
   } elseif ( is_home() || is_singular() ) {
      $title['title'] = single_post_title( '', false );

   // If on a category or tag archive, use the term title.
   } elseif ( is_category() || is_tag() ) {
      $title['title'] = single_term_title( '', false );

   // If on an author archive, use the author's display name.
   } elseif ( is_author() && $author = get_queried_object() ) {
      $title['title'] = $author->display_name;

   // If it's a date archive, use the date as the title.
   } elseif ( is_year() ) {
      $title['title'] = get_the_date( _x( 'Y', 'yearly archives date format' ) );

   } elseif ( is_month() ) {
      $title['title'] = get_the_date( _x( 'F Y', 'monthly archives date format' ) );

   } elseif ( is_day() ) {
      $title['title'] = get_the_date();
   }

   // Add a page number if necessary.
   if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
      $title['page'] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
   }

   // Append the description or site title to give context.
   if ( is_front_page() ) {
      $title['tagline'] = get_bloginfo( 'description', 'display' );
   } else {
      $title['site'] = get_bloginfo( 'name', 'display' );
   }

   /**
    * Filters the separator for the document title.
    *
    * @since 4.4.0
    *
    * @param string $sep Document title separator. Default '-'.
    */
   $sep = apply_filters( 'document_title_separator', '-' );

   /**
    * Filters the parts of the document title.
    *
    * @since 4.4.0
    *
    * @param array $title {
    *     The document title parts.
    *
    *     @type string $title   Title of the viewed page.
    *     @type string $page    Optional. Page number if paginated.
    *     @type string $tagline Optional. Site description when on home page.
    *     @type string $site    Optional. Site title when not on home page.
    * }
    */
   $title = apply_filters( 'document_title_parts', $title );

   $title = implode( " $sep ", array_filter( $title ) );
   $title = wptexturize( $title );
   $title = convert_chars( $title );
   $title = esc_html( $title );
   $title = capital_P_dangit( $title );

   return $title;
}

三、修改single的标题

function single_post_title( $prefix = '', $display = true ) {
   $_post = get_queried_object();

   if ( !isset($_post->post_title) )
      return;

   /**
    * Filters the page title for a single post.
    *
    * @since 0.71
    *
    * @param string $_post_title The single post page title.
    * @param object $_post       The current queried object as returned by get_queried_object().
    */
   $title = apply_filters( 'single_post_title', $_post->post_title, $_post );
   if ( $display )
      echo $prefix . $title;
   else
      return $prefix . $title.'-免费下载,在线阅读';
}

四、修改category和tag的标题

function single_term_title( $prefix = '', $display = true ) {
   $term = get_queried_object();

   if ( !$term )
      return;

   if ( is_category() ) {
      /**
       * Filters the category archive page title.
       *
       * @since 2.0.10
       *
       * @param string $term_name Category name for archive being displayed.
       */
      $term_name = apply_filters( 'single_cat_title', $term->name );
   } elseif ( is_tag() ) {
      /**
       * Filters the tag archive page title.
       *
       * @since 2.3.0
       *
       * @param string $term_name Tag name for archive being displayed.
       */
      $term_name = apply_filters( 'single_tag_title', $term->name );
   } elseif ( is_tax() ) {
      /**
       * Filters the custom taxonomy archive page title.
       *
       * @since 3.1.0
       *
       * @param string $term_name Term name for archive being displayed.
       */
      $term_name = apply_filters( 'single_term_title', $term->name );
   } else {
      return;
   }

   if ( empty( $term_name ) )
      return;

   if ( $display )
      echo $prefix . $term_name;
   else
      return $prefix . $term_name.'-杂志,图书免费下载,在线阅读';
}

五、去掉category标题第一个-(含)后面的内容,并把它显示在body中。

function the_archive_title( $before = '', $after = '' ) {
   $title = get_the_archive_title();

   if ( ! empty( $title ) ) {
      echo $before .substr($title,0,strpos($title, '-')) . $after;
   }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注