Поддержка Проблемы и решения Метатеги на странице вывада записей.

  • Доброго все времени суток
    Очень нужна подсказка.
    Вообщем на сайте только на одной странице нет тега description…
    …и он не добовляется ну никак, на остальных страницах, категориях, записях и т.д. все теги выведены с помощью:

    
    class SEO_Tags {
    	function __construct(){}
    	static function init(){
    		remove_action( 'wp_head', '_wp_render_title_tag', 1 );
    		add_action( 'wp_head', [ __CLASS__, 'render_seo_tags' ], 1 );
    	}
    	static function render_seo_tags(){
    		echo '<title>'. self::meta_title(' — ') .'</title>'."\n\n";
    		echo self::meta_description();
    		echo self::meta_keywords();
    	}
    	static function meta_title( $sep = '»', $add_blog_name = true ){
    		static $cache; if( $cache ) return $cache;
    		global $post;
    		$l10n = apply_filters( 'meta_title_l10n', array(
    			'404'     => 'Error 404: such page does not exist',
    			'search'  => 'Search results for: %s',
    			'compage' => 'Comments %s',
    			'author'  => 'Articles of the author: %s',
    			'archive' => 'Archive for',
    			'paged'   => '(page %d)',
    		) );
    		$parts = array(
    			'prev'  => '',
    			'title' => '',
    			'after' => '',
    			'paged' => '',
    		);
    		$title = & $parts['title'];
    		$after = & $parts['after'];
    		if(0){}
    		elseif ( is_404() ){
    			$title = $l10n['404'];
    		}
    		elseif ( is_search() ){
    			$title = sprintf( $l10n['search'], get_query_var('s') );
    		}
    		elseif( is_front_page() ){
    			if( is_page() && $title = get_post_meta( $post->ID, 'title', 1 ) ){
    			} else {
    				$title = get_bloginfo('name');
    				$after = get_bloginfo('description');
    			}
    		}
    		elseif( is_singular() || ( is_home() && ! is_front_page() ) || ( is_page() && ! is_front_page() ) ){
    			$title = get_post_meta( $post->ID, 'title', 1 );
    			if( ! $title ) $title = apply_filters( 'meta_title_singular', '', $post );
    			if( ! $title ) $title = single_post_title( '', 0 );
    			if( $cpage = get_query_var('cpage') )
    				$parts['prev'] = sprintf( $l10n['compage'], $cpage );
    		}
    		elseif ( is_post_type_archive() ){
    			$title = post_type_archive_title('', 0 );
    			$after = 'blog_name';
    		}
    		elseif( is_category() || is_tag() || is_tax() ){
    			$term = get_queried_object();
    			$title = get_term_meta( $term->term_id, 'title', 1 );
    			if( ! $title ){
    				$title = single_term_title('', 0 );
    				if( is_tax() )
    					$parts['prev'] = get_taxonomy($term->taxonomy)->labels->name;
    			}
    			$after = 'blog_name';
    		}
    		elseif ( is_author() ){
    			$title = sprintf( $l10n['author'], get_queried_object()->display_name );
    			$after = 'blog_name';
    		}
    		elseif ( ( get_locale() === 'en_EN' ) && ( is_day() || is_month() || is_year() ) ){
    			$eng_month  = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    			$eng_month2 = array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    			$year       = get_query_var('year');
    			$monthnum   = get_query_var('monthnum');
    			$day        = get_query_var('day');
    			if( is_year() )      $dat = "$year year";
    			elseif( is_month() ) $dat = "$eng_month[$monthnum] $year years";
    			elseif( is_day() )   $dat = "$day $eng_month2[$monthnum] $year years";
    			$title = sprintf( $l10n['archive'], $dat );
    			$after = 'blog_name';
    		}
    		else {
    			$title = get_the_archive_title();
    			$after = 'blog_name';
    		}
    		$pagenum = get_query_var('paged') ?: get_query_var('page');
    		if( $pagenum )
    			$parts['paged'] = sprintf( $l10n['paged'], $pagenum );
    		$parts = apply_filters_ref_array( 'meta_title_parts', array($parts, $l10n) );
    		if( $after == 'blog_name' )
    			$after = $add_blog_name ? get_bloginfo('name') : '';
    		if( $parts['paged'] ){
    			$parts['title'] .=  " {$parts['paged']}";
    			unset( $parts['paged'] );
    		}
    		$title = implode( ' '. trim($sep) .' ', array_filter($parts) );
    		$title = apply_filters( 'meta_title', $title );
    		$title = wptexturize( $title );
    		$title = esc_html( $title );
    		return $cache = $title;
    	}
    	static function meta_description( $home_description = '', $maxchar = 260 ){
    		static $cache; if( $cache ) return $cache;
    		global $post;
    		$cut   = true;
    		$desc  = '';
    		if( is_front_page() ){
    			if( is_page() && $desc = get_post_meta($post->ID, 'description', true )  ){
    				$cut = false;
    			}
    			if( ! $desc )
    				$desc = $home_description ?: get_bloginfo( 'description', 'display' );
    		}
    		elseif( is_singular() ){
    			if( $desc = get_post_meta($post->ID, 'description', true ) )
    				$cut = false;
    			if( ! $desc ) $desc = $post->post_excerpt ?: $post->post_content;
    			$desc = trim( strip_tags( $desc ) );
    		}
    		elseif( is_category() || is_tag() || is_tax() ){
    			$term = get_queried_object();
    			$desc = get_term_meta( $term->term_id, 'meta_description', true );
    			if( ! $desc )
    				$desc = get_term_meta( $term->term_id, 'description', true );
    			$cut = false;
    			if( ! $desc && $term->description ){
    				$desc = strip_tags( $term->description );
    				$cut = true;
    			}
    		}
    		$origin_desc = $desc;
    		if( $desc = apply_filters( 'meta_description_pre', $desc ) ){
    			$desc = str_replace( array("\n", "\r"), ' ', $desc );
    			$desc = preg_replace( '~\[[^\]]+\](?!\()~', '', $desc );
    			if( $cut ){
    				$char = mb_strlen( $desc );
    				if( $char > $maxchar ){
    					$desc     = mb_substr( $desc, 0, $maxchar );
    					$words    = explode(' ', $desc );
    					$maxwords = count($words) - 1;
    					$desc     = join(' ', array_slice($words, 0, $maxwords)).' ...';
    				}
    			}
    			$desc = preg_replace( '/\s+/s', ' ', $desc );
    		}
    		if( $desc = apply_filters( 'meta_description', $desc, $origin_desc, $cut, $maxchar ) )
    			return $cache = '<meta name="description" content="'. esc_attr( trim($desc) ) .'" />'."\n";
    		return $cache = '';
    	}
    	static function meta_keywords( $home_keywords = '', $def_keywords = '' ){
    		global $post;
    		$out = '';
    		if ( is_front_page() ){
    			$out = $home_keywords;
    		}
    		elseif( is_singular() ){
    			$out = get_post_meta( $post->ID, 'keywords', true );
    			if( ! $out && $post->post_type == 'post' ){
    				$res = wp_get_object_terms( $post->ID, [ 'post_tag', 'category' ], [ 'orderby' => 'none' ] );
    				if( $res && ! is_wp_error($res) )
    					foreach( $res as $tag )
    						$out .= ", $tag->name";
    				$out = ltrim( $out, ', ' );
    			}
    		}
    		elseif ( is_category() || is_tag() || is_tax() ){
    			$term = get_queried_object();
    			if( function_exists('get_term_meta') ){
    				$out = get_term_meta( $term->term_id, "keywords", true );
    			}
    			else{
    				preg_match( '!\[keywords=([^\]]+)\]!iU', $term->description, $match );
    				$out = isset($match[1]) ? $match[1] : '';
    			}
    		}
    		if( $out && $def_keywords )
    			$out = $out .', '. $def_keywords;
    		return $out ? "<meta name=\"keywords\" content=\"$out\" />\n" : '';
    	}
    }
    SEO_Tags::init();
    

    …страница эта — страница вывода записей (указана в настройках wp), я думаю не спроста там этот тег не добавляется.

    Может подкините идейку куда копнуть?

Просмотр 1 ответа (всего 1)
  • …не знаю почему, но страницу вывода записей wp не считает главной is_front_page().
    если добавить
    if( is_home()
    то дескрипшн отображается, но я не уверена правильно ли я выбрала if( is_home()? Потому что это ведь страница записей. Может какое-нибудь условие надо добавить?

    • Ответ изменён 3 года, 8 месяцев назад пользователем milatovskaja.
Просмотр 1 ответа (всего 1)
  • Тема «Метатеги на странице вывада записей.» закрыта для новых ответов.