Php Wordpress:让用户选择每页显示的帖子数量

Php Wordpress:让用户选择每页显示的帖子数量,php,wordpress,Php,Wordpress,我正在尝试构建一个功能,访问者可以选择每页显示的帖子数量,如下所示: 我从哪里开始,如何实现这一点?我目前正在使用query_post列出所有帖子: <?php $paged = 1; query_posts(array( 'showposts'=> 10, 'post_type' => 'post', 'category_name' => 'jobseeker-announcements', 'order' => 'DESC', 'posts_per_page

我正在尝试构建一个功能,访问者可以选择每页显示的帖子数量,如下所示:

我从哪里开始,如何实现这一点?我目前正在使用query_post列出所有帖子:

<?php 
$paged = 1;
query_posts(array( 'showposts'=> 10, 'post_type' => 'post', 'category_name' => 'jobseeker-announcements', 'order' => 'DESC', 'posts_per_page' => 10, 'paged' => get_query_var('paged'))); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post-list">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    <p class="datetime"><span>Date Posted:</span> <?php the_time('m/j/Y'); ?></p>
    <p><?php the_excerpt(); ?></p>
<!--/post-list--></div>
<?php endwhile; ?>

发布日期:


使用jquery
change
函数

 <?php 
if(isset($_GET['pageVal'])){
  $showposts = esc_sql($_GET['pageVal']);
}else{
  $showposts = 10;
}
    $paged = 1;
    query_posts(array( 'showposts'=> $showposts, 'post_type' => 'post', 'category_name' => 'jobseeker-announcements', 'order' => 'DESC', 'posts_per_page' => $posts_per_page, 'paged' => get_query_var('paged'))); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post-list">
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="datetime"><span>Date Posted:</span> <?php the_time('m/j/Y'); ?></p>
        <p><?php the_excerpt(); ?></p>
    <!--/post-list--></div>
    <?php endwhile; ?>

<select class='page-select'>
<option value='10'>10</option>
<option value='10'>20</option>
<option value='10'>30</option>
</select>
<script>
jQuery.ready(function(){
 $('.page-select').change(function(){
  $(location).attr('href', window.location.href+'?pageVal='+$('.page-select').val());
});
});
</script>

发布日期:

10 20 30 就绪(函数(){ $('.page select').change(函数(){ $(location.attr('href',window.location.href+'?pageVal='+$('.page select').val()); }); });
使用jquery
change
函数

 <?php 
if(isset($_GET['pageVal'])){
  $showposts = esc_sql($_GET['pageVal']);
}else{
  $showposts = 10;
}
    $paged = 1;
    query_posts(array( 'showposts'=> $showposts, 'post_type' => 'post', 'category_name' => 'jobseeker-announcements', 'order' => 'DESC', 'posts_per_page' => $posts_per_page, 'paged' => get_query_var('paged'))); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post-list">
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
        <p class="datetime"><span>Date Posted:</span> <?php the_time('m/j/Y'); ?></p>
        <p><?php the_excerpt(); ?></p>
    <!--/post-list--></div>
    <?php endwhile; ?>

<select class='page-select'>
<option value='10'>10</option>
<option value='10'>20</option>
<option value='10'>30</option>
</select>
<script>
jQuery.ready(function(){
 $('.page-select').change(function(){
  $(location).attr('href', window.location.href+'?pageVal='+$('.page-select').val());
});
});
</script>

发布日期:

10 20 30 就绪(函数(){ $('.page select').change(函数(){ $(location.attr('href',window.location.href+'?pageVal='+$('.page select').val()); }); });
可能是您解决方案的完美代码

    <?php 

    if(isset($_GET['page'])){
        $posts_per_page = esc_sql($_GET['page']);
    } else {
         $posts_per_page = 10;
    }

    $paged = ( get_query_var( 'paged')) ? get_query_var( 'paged') : 1;

    query_posts(
        array( 
             'post_type' => 'post',
             'category_name' => 'jobseeker-announcements',
             'order' => 'DESC', 
             'posts_per_page' => $posts_per_page,
             'paged' => get_query_var('paged'))); 

    while (have_posts()) : the_post(); ?>

        <div class="post-list">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <p class="datetime"><span>Date Posted:</span> <?php the_time('m/j/Y'); ?></p>
            <p><?php the_excerpt(); ?></p>
        </div>

    <?php endwhile; ?>

    <select class="page-select">
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="20">30</option>
    </select>


    <script>

    jQuery.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>

发布日期:

10 20 30 就绪(函数(){ $('.page select').change(函数(){ var checkIndex=window.location.href.indexOf(“?”); 如果(检查索引>-1){ var Link=window.location.href.substr(0,checkIndex-1); $(location.attr('href',Link+'?page='+$('.page select').val()); }否则{ $(location.attr('href',window.location.href+'?page='+$('.page select').val()); } }); });
可能是您解决方案的完美代码

    <?php 

    if(isset($_GET['page'])){
        $posts_per_page = esc_sql($_GET['page']);
    } else {
         $posts_per_page = 10;
    }

    $paged = ( get_query_var( 'paged')) ? get_query_var( 'paged') : 1;

    query_posts(
        array( 
             'post_type' => 'post',
             'category_name' => 'jobseeker-announcements',
             'order' => 'DESC', 
             'posts_per_page' => $posts_per_page,
             'paged' => get_query_var('paged'))); 

    while (have_posts()) : the_post(); ?>

        <div class="post-list">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <p class="datetime"><span>Date Posted:</span> <?php the_time('m/j/Y'); ?></p>
            <p><?php the_excerpt(); ?></p>
        </div>

    <?php endwhile; ?>

    <select class="page-select">
        <option value="10">10</option>
        <option value="20">20</option>
        <option value="20">30</option>
    </select>


    <script>

    jQuery.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>

发布日期:

10 20 30 就绪(函数(){ $('.page select').change(函数(){ var checkIndex=window.location.href.indexOf(“?”); 如果(检查索引>-1){ var Link=window.location.href.substr(0,checkIndex-1); $(location.attr('href',Link+'?page='+$('.page select').val()); }否则{ $(location.attr('href',window.location.href+'?page='+$('.page select').val()); } }); });
默认情况下,每页显示10篇文章。如果您希望以访客身份显示文章,则需要获得自定义WordPress查询以及自定义分页,使用get方法,您可以设置每页文章和当前分页,这将在查询限制中使用。默认情况下,如果您希望以访客身份显示文章,则需要获得10篇文章自定义WordPress查询以及自定义分页和使用get方法,您可以设置每页的帖子和将在查询限制中使用的当前分页。不过,我做了一些修改,因为出于某种原因,如果(isset($get['pageVal']){$pages=$get['pageVal'];$showposts=$pages;},它无法正确获取pageVal的值else{$showposts=10;}使用esc_sql函数避免sql注入。不过我做了一点修改,因为它由于某种原因无法正确获取pageVal的值,如果(isset($\u GET['pageVal']){$pages=$\u GET['pageVal'];$showposts=$pages;}使用esc_sql函数避免sql注入。