ぬの部屋(仮)
nu-no-he-ya
  • 1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
       1234
    567891011
    12131415161718
    19202122232425
    26272829   
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28      
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
    1234567
    891011121314
    15161718192021
    22232425262728
           
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
         12
    3456789
    10111213141516
    17181920212223
    242526272829 
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
        123
    45678910
    11121314151617
    18192021222324
    25262728   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    15161718192021
    293031    
           
         12
    3456789
    10111213141516
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    30      
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    2627282930  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728     
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
        123
    45678910
    11121314151617
    18192021222324
    252627282930 
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
     123456
    78910111213
    14151617181920
    21222324252627
    28293031   
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
     123456
    78910111213
    14151617181920
    21222324252627
    282930    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31      
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
      12345
    6789101112
    13141516171819
    20212223242526
    27282930   
           
          1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031     
          1
    2345678
    9101112131415
    16171819202122
    232425262728 
           
       1234
    567891011
    12131415161718
    19202122232425
    262728293031 
           
    1234567
    891011121314
    15161718192021
    22232425262728
    293031    
           
         12
    3456789
    10111213141516
    17181920212223
    24252627282930
           
      12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
           
    1234567
    891011121314
    15161718192021
    22232425262728
    2930     
           
        123
    45678910
    11121314151617
    18192021222324
    25262728293031
           
  • the_posts_paginationで「投稿ナビゲーション」を出力しない

    the_posts_paginationを使えば関数呼び出し一発でページネーションを表示できます。

    <?php
      global $wp_query;
      $total_results = $wp_query->found_posts;
      $search_query = get_search_query();
    ?>
     
    <h1><?php echo $search_query; ?>の検索結果</h1>
    <div><?php get_search_form(); ?></div>
     
    <?php
    if( $total_results >0 ):
      if(have_posts()):
        while(have_posts()): the_post();
    ?>
    
    
        <p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
        <div class="contents">
          <?php the_content(); ?>
        </div>
    
     
    <?php 
        endwhile; 
      endif; 
    	the_posts_pagination( array('screen_reader_text'=>'') );
    else: 
      echo $search_query;
      echo 'に一致する情報は見つかりませんでした。';
    
    endif;
    
    ?>
    

     

    問題はその出力に、「投稿ナビゲーション」というラベルがついてしまうことです。

    pagination_h2

    このラベルを消したいとします。

     

     

     

    ただ単に非表示にするならいくつか方法があります。この出力は、以下のようにh2にscreen-reader-textというクラスがついています。

    <h2 class="screen-reader-text">投稿ナビゲーション</h2>
    

     

     

    この表示をCSS等で切ってしまえば良いことになります。

    .screen-reader-text{
      display:none;
    }
    

     

     

    あるいはもっと楽なのが、

    the_posts_pagination( array('screen_reader_text'=>' ') );
    

    と、the_posts_pagination関数のscreen_reader_textパラメータにスペース一個を渡してやると、スペースが表示されるために見た目上きえます。

     

    ちなみに、見つけた限り最高の正攻法は、https://kimoba.com/kasegu/the_posts_pagination.htmlさんの、get_the_posts_paginationの結果から文字列置換で取り除いてしまうやりかたです。

     

     

     

     

    ただ、それでは面白くないので、もう少し無駄なことをします。まずcodexへ行きます。

    https://codex.wordpress.org/Function_Reference/the_posts_pagination

    link_to_get_the_pagination

    Defined in wp-includes/link-template.php という記述があるので、実装を見て見ます。

    the_posts_paginationget_the_posts_pagination_navigation_markup

    という順番で文字列が生成されているので、_navigation_markupを見てみます。

    function _navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
      if ( empty( $screen_reader_text ) ) {
        $screen_reader_text = __( 'Posts navigation' );
      }
    
      $template = '
        <nav class="navigation %1$s" role="navigation">
        <h2 class="screen-reader-text">%2$s</h2>
        <div class="nav-links">%3$s</div>
        </nav>';
    
      return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
    }
    

    ありました。この<h2 class="screen-reader-text">%2$s</h2>がこのタイトルの本体です。こいつをそのまま削ってしまえば良いことになります。とはいえ、Wordpressのコアを書き換えるほどクレイジーにはなれないので、

    • get_the_posts_paginationmy_get_the_posts_pagination
    • _navigation_markup_my_navigation_markup

    と名前を変えて、上記h2の部分だけを取り除いた各関数をfunctions.phpに定義して呼び出すことにします。

     

    <?php
    //ページネーション
    function my_get_the_posts_pagination( $args = array() ) {
      $navigation = '';
    
      // Don't print empty markup if there's only one page.
      if ( $GLOBALS['wp_query']->max_num_pages > 1 ) {
        $args = wp_parse_args( $args, array(
          'mid_size'           => 1,
          'prev_text'          => __( 'Previous' ),
          'next_text'          => __( 'Next' ),
          'screen_reader_text' => __( 'Posts navigation' ),
        ) );
    
        // Make sure we get a string back. Plain is the next best thing.
        if ( isset( $args['type'] ) && 'array' == $args['type'] ) {
          $args['type'] = 'plain';
        }
    
        // Set up paginated links.
        $links = paginate_links( $args );
    
        if ( $links ) {
          $navigation = _my_navigation_markup( $links, 'pagination', $args['screen_reader_text'] );
        }
      }
    
        return $navigation;
    }
    
    
    
    function _my_navigation_markup( $links, $class = 'posts-navigation', $screen_reader_text = '' ) {
      if ( empty( $screen_reader_text ) ) {
        $screen_reader_text = __( 'Posts navigation' );
      }
    
      $template = '
        <nav class="navigation %1$s" role="navigation">
        <div class="nav-links">%3$s</div>
        </nav>';
    
      return sprintf( $template, sanitize_html_class( $class ), esc_html( $screen_reader_text ), $links );
    }
    //ページネーションここまで
    

     

    呼び出しはget_the_posts_paginationと同じです。the_posts_paginationと全く同じにしたいならechoする関数をもう一つ被せる必要があります。

    <?php
    echo my_get_the_posts_pagination( );
    ?>