wordpress删除默认的title tag添加自己的title

Published
2022-07-20
浏览次数 :  151

我们可以通过去除action的形式来删除掉默认的title tag:

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

还有更好的方式可以在子主题上 删除掉title-tag的 theme support .

remove_theme_support( 'title-tag' )

然后添加自己形式的title规则:

global $new_title;
	if( $new_title ) return $new_title;

	global $paged;

	$html = '';
	$t = trim(wp_title('', false));

	if( _hui('seo_off') ){
		return $t;
	}

	if( (is_single() || is_page()) && get_the_subtitle(false) ){
		$t .= get_the_subtitle(false);
	}

	if( is_single() && _hui('post_title_and_catname_s') ){
		$category = get_the_category();
        if($category && $category[0]){
            $t .= _get_delimiter() . $category[0]->cat_name;
        }
	}

	if ($t) {
		$html .= $t . _get_delimiter();
	}

	$html .= get_bloginfo('name');

	if (is_home()) {
		if(_hui('hometitle')){
            $html = _hui('hometitle');
            if ($paged > 1) {
                $html .= _get_delimiter() . '最新发布';
            }
        }else{
			if ($paged > 1) {
				$html .= _get_delimiter() . '最新发布';
			}else if( get_option('blogdescription') ){
				$html .= _get_delimiter() . get_option('blogdescription');
			}
		}
	}

	if( is_category() ){
		global $wp_query; 
		$cat_ID = get_query_var('cat');
		$cat_tit = _get_tax_meta($cat_ID, 'title');
		if( $cat_tit ){
			$html = $cat_tit;
		}
	}

	if( is_tag() ){
		global $wp_query; 
		$tag_ID = get_query_var('tag_id');
		$tag_tit = _get_tax_meta($tag_ID, 'title');
		if( $tag_tit ){
			$html = $tag_tit;
		}
	}

	if ( is_tax() ) { 
        global $wp_query;
        $data = $wp_query->get_queried_object();

        $title = _get_tax_meta($data->term_id, 'title');

        if( $title ){
            $html = $title;
        }else{
            $html = $data->name ._get_delimiter(). get_bloginfo('name');
        }
    }

	if( (is_single() || is_page()) && _hui('post_keywords_description_s') ){
		global $post;
	    $post_ID = $post->ID;
	    $seo_title = trim(get_post_meta($post_ID, 'title', true));
		if($seo_title){
			$html = $seo_title;
		}elseif( _hui('post_title_no_sitetitle_s') && is_single() ){
			$html = $t;
		}
	}

	if ($paged > 1) {
		$html .= _get_delimiter() . '第' . $paged . '页';
	}

	return $html;

然后head.php里添加自己的title

<title><?php echo _title(); ?></title>

  • 标签1
  • 标签1
  • 标签1
Top