让用户选择每个页面jQuery错误显示的帖子数

时间:2018-04-16 作者:Spencer Shattuck

我希望用户能够选择每页显示10、20或30篇文章。

我找到了这个Let user change posts per page 但它不起作用。

我发现this 并实现了它,但我得到了一个jquery错误。Uncaught TypeError: e.indexOf is not a function

这是我的代码:(我正在使用Bootstrap 4)

<?php

  if( isset($_GET[\'pageVal\']) ) {
    $showposts = esc_sql($_GET[\'pageVal\']);
  } else {
    $showposts = 10;
  }

  $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
  $newsposts = array(
    \'posts_per_page\' => $showposts,
    \'paged\' => $paged,
    \'category_name\' => \'News\'
  );
  query_posts($newsposts);
?>
<?php if ( have_posts(\'newsposts\') ) : ?>
<section class="blue">
  <div class="container">
    <div class="row">
      <div class="col d-flex justify-content-start px-sm-4 white-text">
        <span class="font-weight-bold p-2 mr-3">Title Filter</span>
        <?php echo do_shortcode(\'[wpdreams_ajaxsearchlite]\'); ?>
        <span class="font-weight-bold p-2">Display #</span>
        <select id="news-posts-per-page" class="page-select">
          <option value="10">10</option>
          <option value="20">20</option>
          <option value="30">30</option>
        </select>
        <script>
        jQuery(document).ready(function( $ ) {
          $(\'.page-select\').change(function(){
            var checkIndex = window.location.href.indexOf(\'?\');
            if( checkIndex > -1 ){
              var Link = window.location.href.substr(0, checkIndex-1);
              $(location).attr(\'href\', Link+\'?page=\'+$(\'.page-select\').val());
            } else {
              $(location).attr(\'href\', window.location.href+\'?page=\'+$(\'.page-select\').val());
            }
          });

        });
        </script>
        <span class="font-weight-bold pl-1 py-2"><i class="fas fa-sort"></i></span>
      </div>
    </div>
  </div>
</section>

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

一个简单的修复方法是更改查询名称;i、 e.来自pagepageVal.

<script>
jQuery(document).ready(function( $ ) {
  $(\'.page-select\').change(function(){
    var checkIndex = ( window.location.href || \'\' ).indexOf(\'?\');
    if( checkIndex > -1 ){
      var Link = window.location.href.substr(0, checkIndex-1);
      $(location).attr(\'href\', Link+\'?pageVal=\'+$(\'.page-select\').val());
    } else {
      $(location).attr(\'href\', window.location.href+\'?pageVal=\'+$(\'.page-select\').val());
    }
  });

});
</script>
但我会用这个

<script>
jQuery(document).ready(function( $ ) {
  $(\'.page-select\').change(function(){
    var url = window.location.href || \'\',
      has_q = ( url.indexOf( \'?\' ) > -1 );

    if ( url.indexOf( \'?pageVal=\' ) > -1 ) {
      url = url.replace( /\\?pageVal=\\d+/, \'?pageVal=\' + this.value );
    } else if ( has_q && url.indexOf( \'&pageVal=\' ) > -1 ) {
      url = url.replace( /&pageVal=\\d+/, \'&pageVal=\' + this.value );
    } else {
      url = url + ( has_q ? \'&\' : \'?\' ) + \'pageVal=\' + this.value;
    }

    window.location.href = url;
  });

});
</script>

Additional Note

我也会改变select 收件人:

<select id="news-posts-per-page" class="page-select"><?php
  // List of "posts per page" options.
  $per_pages = [ 10, 20, 30 ];

  foreach ( $per_pages as $n ) {
    $selected = selected( $n, $showposts, false );
    printf( \'<option value="%d"%s>%d</option>\',
      $n, $selected, $n );
  }
?></select>

结束