【问题标题】:Wordpress query with pagination带分页的 Wordpress 查询
【发布时间】:2017-08-07 11:09:48
【问题描述】:

我需要你的帮助。我有一个自定义搜索引擎,可以从帖子类型分类中搜索产品:

if( isset($_POST['search_products'] ) {
    /// codes ....
    $_SESSION['ids'] = $my_ids;
    $args = array(
        'post_type' => 'product',
        'showposts' => -1,
        'post__in' => $_SESSION['ids']
    )
    $posts = new Wp_Query($args);
}

这个查询输出了大约 60 个带有分页的产品(每页 10 个产品),但是当用户在没有使用搜索引擎的情况下访问该页面时,应该显示所有产品。相反,$_SESSION 保留并仅显示以前的结果。

我只想在搜索时分页工作,并且在我访问页面时显示所有产品而不使用搜索引擎。

是否有任何 Wordpress 专家有想法?

谢谢。

【问题讨论】:

标签: php wordpress pagination


【解决方案1】:
if( isset($_POST['search_products'] ) {
    /// codes ....
    $_SESSION['ids'] = $my_ids;
    $args = array(
        'post_type' => 'product',
        'showposts' => -1,
        'post__in' => $_SESSION['ids']
    )
    $posts = new Wp_Query($args);
}
else {

$args = array(
        'post_type' => 'product',
        'showposts' => -1,
    )
    $posts = new Wp_Query($args);
}

只需放置一个 else 块

【讨论】:

  • 或者更简洁: $args = array( 'post_type' => 'product', 'showposts' => -1, ) if ( isset( $_POST['search_products'] ) ) { $ _SESSION['ids'] = $my_ids; $args['post__in'] = $_SESSION['ids']; } $posts = new Wp_Query( $args );
  • 您好,谢谢,您是对的,但我也必须以 $_GET 形式发送表单,它可以解决问题。
【解决方案2】:

代码,

global $post;
$id   = intval($_GET['cat']);
$paged     = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$products  = new WP_Query( array( 
                                'post_type'      => 'products', 
                                'order'          => 'ASC',
                                'posts_per_page' => 5,
                                'paged'          => $paged
                         ) );
endwhile;

HTML ,

<ul class="pagination pull-right">
 <li><?php echo get_next_posts_link( 'Next Page', $products->max_num_pages ); ?></li>
 <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多