搜索结果页面上的分页

时间:2017-01-16 作者:RomanT

我目前正在WordPress上建立一个网站。该网站具有自定义帖子类型。我有一个自定义的搜索表单,它从自定义帖子类型中选择条目。条目正在按预期进行选择和排序,但是在我的搜索结果页面(job search.php)上,我需要分页,但它不起作用,尽管相同的代码在其他页面上也可以正常工作。我在浏览器的搜索栏中看到页码变化,但我总是看到第一页的条目。

具有指定选择和筛选的自定义职位类型的搜索表单和查询代码(在job search.php中):

<?php
/*
 * Template Name:job-search
 */
get_header();?>

<div>
    <form method="get" action="<?php echo site_url(\'/\'); ?>/job-search" >
        <input type="search" name="job-name" placeholder="Job..." />
        <input type="search" name="town-name" placeholder="Town..." />
        <select type="search" name="date">
            <option value="DESC">Datum absteigend</option>
            <option value="ASC">Datum aufsteigend</option>      
        </select>
        <div>
            <input type="checkbox" name="cat[]" value="IT, Telekommunikation" />IT, Telekommunikation
            <input type="checkbox" name="cat[]" value="Ingenieur, Technik" />Ingenieur, Technik
            <input type="checkbox" name="cat[]" value="Handwerk, Gewerbe" />Handwerk, Gewerbe
        </div>
        <input type="submit" value="Search">
    </form>
</div>

<?php
    $jobName = $_GET[\'job-name\'];
    $jobTown = $_GET[\'town-name\'];
    $jobDate = $_GET[\'date\'];

    if($_GET[\'cat\'] && !empty($_GET[\'cat\'])){
        $jobCat = $_GET[\'cat\'];
    }
    else{
        $jobCat = array(\'Vertrieb, Verkauf\', \'IT, Telekommunikation\', \'Ingenieur, Technik\', \'Personalwesen\', \'Bildung, Soziales\', \'Einkauf, Logistik\', \'Handwerk, Gewerbe\', \'Führungskräfte\', \'Marketing, PR\', \'Medizin, Pflege\', \'Finanzen, Steuern, Recht\', \'Kaufmännische Berufe\', \'Luft- und Raumfahrt, Reise\', \'Ausbildung\', \'\');
    }
?>
<h1 style="font-size:1.5em; font-weight:bold; border-bottom:2px solid green; color:green; margin-bottom:20px;">wp_query_search results</h1>
<?php
    $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
    $q1 = array(
        \'post_type\' => \'jobs\',
        \'s\' => $jobName,
        \'posts_per_page\' => 10,
        \'paged\' => $paged,
        \'meta_key\' => \'date\',
        \'orderby\' => \'meta_value\',
        \'order\' => $jobDate,
        \'meta_query\' => array (
            \'relation\' => \'AND\',
            array(
                \'key\' => \'town\',
                \'value\' => $jobTown,
                \'compare\' => \'LIKE\'
            ),
            array(
                \'relation\' => \'OR\',
                array(
                    \'key\' => \'cat_1\',
                    \'value\' => $jobCat
                ),
                array(
                    \'key\' => \'cat_2\',
                    \'value\' => $jobCat
                )
            )
        )
    );
    $query = new WP_Query($q1);
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();?>
        <h2 style="color:brown; font-weight:bold; border-bottom:1px dotted brown;"><?php the_field(\'title\'); ?></h2>
        <h3><?php the_field(\'town\'); ?></h3>
        <h4><?php the_field(\'cat_1\'); ?> | <?php the_field(\'cat_2\'); ?></h4>
        <h5><?php the_field(\'date\'); ?></h5>
    <?php endwhile;

    if (function_exists(search_pagination)) {
        search_pagination($query->max_num_pages,"",$paged);
    }

    endif; wp_reset_query(); ?>

</div>
<?php get_footer(); ?>
分页代码(在functions.php内部):

function search_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\') {
  if (empty($pagerange)) {
    $pagerange = 2;
  }
  $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == \'\') {
    //global $wp_query;
    //$numpages = $wp_query->max_num_pages;
    $numpages = $query->max_num_pages;
    if(!$numpages) {
        $numpages = 1;
    } 
  }
  $pagination_args = array(
    \'base\'            => get_pagenum_link(1) . \'%_%\',
    \'format\'          => \'/page/%#%\',
    \'total\'           => $numpages,
    \'current\'         => $paged,
    \'show_all\'        => False,
    \'end_size\'        => 1,
    \'mid_size\'        => $pagerange,
    \'prev_next\'       => True,
    \'prev_text\'       => __(\'&laquo;\'),
    \'next_text\'       => __(\'&raquo;\'),
    \'type\'            => \'plain\',
    \'add_args\'        => false,
    \'add_fragment\'    => \'\'
  );

  $paginate_links = paginate_links($pagination_args);

  if ($paginate_links) {
    echo "<nav class=\'custom-pagination\'>";
      echo $paginate_links;
    echo "</nav>";
  }
}
我真的需要帮助和建议。提前多谢!

2 个回复
SO网友:Jayesh

从你的求职中。php替换\'posts_per_page\' => 10,\'posts_per_page\' => 3,

只需在您的搜索页面中添加以下代码,我指的是您在哪里搜索帖子。

the_posts_pagination( array( \'mid_size\'  => 2 ) );

SO网友:socki03

所有的东西看起来都像我用过的一样。唯一的事情是,我相信function_exists 参数needs to pass a string. 它应该是:

if ( function_exists(\'search_pagination\') ) {
看起来您使用的方式不会将其注册为字符串。

相关推荐

使用WP_QUERY混合发布日期和发布元值

正在寻找使用自定义字段将旧帖子推送到WP\\u查询中的方法。例如,我今年有10篇文章,想将过去的2篇文章重新发布到自定义rss提要。因此,对于旧帖子,必须按照帖子日期和帖子元中的值对帖子进行排序:new post - (post date: 2018-11-11) new post - (post date: 2018-11-10) old post - (post date: 2017-05-01, post-meta date: 2018-11-09) new post - (