Wordpress分页从自定义循环传递变量

时间:2018-11-29 15:25:22

标签: php wordpress

我尝试在wordpress中使用分页时尽可能传递一组变量,甚至循环本身。当前,主页循环和分页工作正常,但是当用户提交自定义查询并转到结果的第二页时,构成查询的变量不会被继承。

我相信您可以使用url传递这些变量,但这不会使网站受到攻击吗?

下面是我要查询的内容,因为根据区域,通过选择或输入按钮输入自定义表单:

<?php 
    $typename = $_REQUEST["project_type_select"];
    $sectorname = $_REQUEST["project_sector_select"];
    $typenametrim = trim($typename);
    $sectornametrim = trim($sectorname);
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

            if (isset($_REQUEST["project_type_select"]))
            {
                $tax_query =  array(
                        'taxonomy' => 'project_type',
                        'field' => 'slug',
                        'terms' => $typenametrim
                );
            }
            if (isset($_REQUEST["project_sector_select"]))
            {
                $tax_query =  array(
                        'taxonomy' => 'project_sector',
                        'field' => 'slug',
                        'terms' => $sectornametrim
                );
            }

            $loop = new WP_Query( 
                array(
                    //Retreive ALL course posts
                    'post_type' => 'our_projects',
                    'posts_per_page' => 8, 
                    'orderby' => 'title', 
                    'order' => 'ASC', 

                    'paged' => $paged,
                    'tax_query' => array( 
                        $tax_query
                     ),

                )
             );
            global $wp_query;
            // Put default query object in a temp variable
            $tmp_query = $wp_query;
            // Now wipe it out completely
            $wp_query = null;
            // Re-populate the global with our custom query
            $wp_query = $loop;
            while ( $loop->have_posts() ) : $loop->the_post();
        ?>

对于我的实际分页,我有:

<?php
     echo paginate_links( $wp_query );
     wp_reset_postdata();
?>

0 个答案:

没有答案