我目前正在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\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'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>";
}
}
我真的需要帮助和建议。提前多谢!