Главная WordPress

WordPress: избегаем дублирования заголовков на страницах комментариев

Начиная с WordPress 2.7 появилась возможность разбивать комментарии на страницы (ранее приходилось делать это с помощью сторонних плагинов). Однако при этом разработчики не предусмотрели следующую вещь — независимо от такого, на какой странице комментариев мы находимся, в заголовке окна браузера (тег <title></title>) отображается один и тот же текст. А это, как мы знаем, не есть хорошо с точки зрения поисковой оптимизации сайта.

Чтобы устранить этот недочет вордпрессовцев, необходимо выполнить всего одно простое действие — в файле header.php вашей темы найти закрывающийся тег </title> и перед ним поставить следующий код:

<?php if ( $cpage > 0 ) { echo ' - страница комментариев ' . $cpage; } ?>

После этого заголовок каждой страницы с комментариями (за исключением первой) приобретет примерно следующий вид:

Заголовок страницы — страница комментариев 2

Цифра, естественно, будет соответствовать номеру страницы комментариев, на которой мы находимся.

Комментарии (5)

  1. <head>
      <meta charset="<?php bloginfo('charset'); ?>">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="profile" href="<?php echo esc_url( 'gmpg.org/xfn/11' ); ?>">
      <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
    
      <?php wp_head(); ?>
    </head>
    
    

    Как быть? Title зашит в wp_head ()

  2. Вот нашел код, он решает подобный вопрос, но со страницами, сюда бы еще для комментариев добавить

    // Custom Page Titles
    add_action('admin_menu', 'custom_title');
    add_action('save_post', 'save_custom_title');
    add_action('wp_head','insert_custom_title');
    function custom_title() {
    	add_meta_box('custom_title', 'Change page title', 'custom_title_input_function', 'post', 'normal', 'high');
    	add_meta_box('custom_title', 'Change page title', 'custom_title_input_function', 'page', 'normal', 'high');
    }
    function custom_title_input_function() {
    	global $post;
    	echo '<input type="hidden" name="custom_title_input_hidden" id="custom_title_input_hidden" value="'.wp_create_nonce('custom-title-nonce').'" />';
    	echo '<input type="text" name="custom_title_input" id="custom_title_input" style="width:100%;" value="'.get_post_meta($post->ID,'_custom_title',true).'" />';
    }
    function save_custom_title($post_id) {
    	if (!wp_verify_nonce($_POST['custom_title_input_hidden'], 'custom-title-nonce')) return $post_id;
    	if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) return $post_id;
    	$customTitle = $_POST['custom_title_input'];
    	update_post_meta($post_id, '_custom_title', $customTitle);
    }
    function insert_custom_title() {
    	if (have_posts()) : the_post();
    	  $customTitle = get_post_meta(get_the_ID(), '_custom_title', true);
    	  if ($customTitle) {
    		echo "<title>$customTitle</title>";
          } else {
        	echo "<title>";
    	      if (is_tag()) {
    	         single_tag_title("Tag Archive for &quot;"); echo '&quot; - '; }
    	      elseif (is_archive()) {
    	         wp_title(''); echo ' Archive - '; }
    	      elseif ((is_single()) || (is_page()) &amp;&amp; (!(is_front_page())) ) {
    	         wp_title(''); echo ' - '; }
    	      if (is_home()) {
    	         bloginfo('name'); echo ' - '; bloginfo('description'); }
    	      else {
    	          bloginfo('name'); }
    	      if ($paged>1) {
    	         echo ' - page '. $paged; }
            echo "</title>";
        }
        else :
          echo "<title>Page Not Found | Envision</title>";
    	endif;
    	rewind_posts();
    }
    
  3. Спасибо за ответ, но увы все без изменений на страницах с комментариями

    // Custom Page Titles
    add_action('admin_menu', 'custom_title');
    add_action('save_post', 'save_custom_title');
    add_action('wp_head','insert_custom_title');
    function custom_title() {
    	add_meta_box('custom_title', 'Change page title', 'custom_title_input_function', 'post', 'normal', 'high');
    	add_meta_box('custom_title', 'Change page title', 'custom_title_input_function', 'page', 'normal', 'high');
    }
    function custom_title_input_function() {
    	global $post;
    	echo '<input type="hidden" name="custom_title_input_hidden" id="custom_title_input_hidden" value="'.wp_create_nonce('custom-title-nonce').'" />';
    	echo '<input type="text" name="custom_title_input" id="custom_title_input" style="width:100%;" value="'.get_post_meta($post->ID,'_custom_title',true).'" />';
    }
    function save_custom_title($post_id) {
    	if (!wp_verify_nonce($_POST['custom_title_input_hidden'], 'custom-title-nonce')) return $post_id;
    	if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) return $post_id;
    	$customTitle = $_POST['custom_title_input'];
    	update_post_meta($post_id, '_custom_title', $customTitle);
    }
    function insert_custom_title() {
    	if (have_posts()) : the_post();
    	  $customTitle = get_post_meta(get_the_ID(), '_custom_title', true);
    	  if ($customTitle) {
    		echo "<title>$customTitle</title>";
          } else {
        	echo "<title>";
    	      if (is_tag()) {
    	         single_tag_title("Tag Archive for &quot;"); echo '&quot; - '; }
    	      elseif (is_archive()) {
    	         wp_title(''); echo ' Archive - '; }
    	      elseif ((is_single()) || (is_page()) &amp;&amp; (!(is_front_page())) ) {
    	         wp_title(''); echo ' - '; }
    	      if (is_home()) {
    	         bloginfo('name'); echo ' - '; bloginfo('description'); }
    	      else {
    	          bloginfo('name'); }
    	      if ($paged>1) {
    	         echo ' - page '. $paged; }
                  
                  if ( $cpage > 1 ) { 
                      echo ' - страница комментариев ' . $cpage; }
    
            echo "</title>";
        }
        else :
          echo "<title>Page Not Found | Envision</title>";
    	endif;
    	rewind_posts();
    }
    

Ваш комментарий