php - 类别和标签的 Wordpress 分页问题
问题描述
我有一个 WordPress 页面来列出带有类别和标签过滤器的帖子。我的分页有问题,即当我们在第二页并单击任何类别时,分页保持不变。如果我更改类别或关键字,则始终 1 应该处于活动状态。而且,如果我们在第一页并选择任何类别并转到未选择类别的第二页。我对WordPress不太熟悉。有没有办法解决这个问题?谢谢你
$currentPage = get_query_var('paged');
// General arguments
if (isset($_POST['category'])) {
$selectedCategories = $_POST['category']; // outputs the array with selected category;
$currentPage = 0;
}
if (isset($_POST['tags'])) {
$selectedTags = $_POST['tags'];// outputs the array with selected tags
$currentPage = 0; // set page is 0
}
$posts = new WP_Query(array(
'category__and' => $selectedCategories,
'tag__in' => $selectedTags,
'post_type' => 'post', // Default or custom post type
'posts_per_page' => 5, // Max number of posts per page
'paged' => $currentPage
));
$categoryTitle = !empty(get_field( "category_title", $post->ID )) ? get_field( "category_title", $post->ID ) : 'Kategorien';
$tagTitle = !empty(get_field( "tag_title", $post->ID )) ? get_field( "tag_title", $post->ID ) : 'Schlagworte';
?>
<?php
//banner image
if (has_post_thumbnail($post->ID)) : ?>
<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail'); ?>
<div id="banner-img" style="background-image: url('<?php echo $image[0]; ?>')">
<h1 class="pb-3"><?php the_title(); ?></h1>
</div>
<?php endif; ?>
<div class="ast-container">
<div class="ast-row">
<div class="ast-col-md-8 ast-col-lg-9 crm-know-how-outer">
<?php
// Content display
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post(); ?>
<div class="post-wrap">
<div class="ast-row">
<!-- Left Imag Starts -->
<div class="ast-col-sm-4 ast-col-md-4 ast-col-lg-4 crm-know-how-left">
<?php
if (wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()))) { ?>
<a href="<?php echo get_permalink(get_the_ID()); ?>">
<img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id(get_the_ID())); ?>">
</a>
<?php
} else { ?>
<a href="<?php echo get_permalink(get_the_ID()); ?>">
<div class="no-image">
<span>Kein Bild</span>
</div>
</a>
<?php }
?>
</div>
<!-- Left Imag Ends -->
<!-- Right Content Starts -->
<div class="ast-col-sm-8 ast-col-md-8 ast-col-lg-8 crm-know-how-right">
<h2>
<a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a>
</h2>
<div class="cs-date">
<?php echo get_the_date(); ?>
</div>
<div class="details-content">
<?php
if (strlen(get_the_content()) > 230) {
echo (substr(get_the_content(), 0, 230)).'...';
} else {
echo (substr(get_the_content(), 0, 230));
}
?>
</div>
<a href="<?php echo get_permalink(get_the_ID()); ?>" class="elementor-button-link elementor-button elementor-size-md">Mehr</a>
</div>
<!-- Right Content Ends -->
</div>
</div>
<?php
}
} else {
// if no posts find!!!
?>
<div class="post-wrap">
<label>Verzeihung, keine Artikel passen auf deine Kriterien.</label>
</div>
<?php
}
//start pagination
?>
<div class='page-nav-container'>
<?php echo(
paginate_links(array(
'total' => $posts->max_num_pages,
'prev_text' => __('<') ,
'next_text' => __('>')
))); ?>
</div>
</div>
<div class="ast-col-md-4 ast-col-lg-3 right-categories">
<?php
//category list start
$categories = get_categories();
$posttags = get_tags();
?>
<form action="" method="post">
<ul>
<label class="categories-head"><?php echo ($categoryTitle); ?></label>
<?php
if ($categories) {
foreach ($categories as $category) {
if (in_array($category->term_id, $selectedCategories)) {
$checked = "checked";
$class = 'active';
} else {
$checked = "";
}
?>
<li>
<label class="check">
<input type="checkbox" class="cs-category <?php echo($class); ?>" name="category[]" <?php echo ($checked); ?> value="<?php echo $category->term_id; ?>" onChange="this.form.submit()">
<span class="checkmark"></span>
<label><?php echo $category->cat_name; ?></label>
</label>
</li>
<?php
}
}
?>
</ul>
<ul class="cs-tag-wraper">
<label class="categories-head"><?php echo($tagTitle);?></label>
<?php
if ($posttags) {
foreach ($posttags as $tags) {
if (in_array($tags->term_id, $selectedTags)) {
$checked = "checked";
$class = 'active';
} else {
$checked = "";
$class = '';
}
?>
<li>
<label class="check">
<input type="checkbox" class="cs-tag <?php echo($class); ?>" name="tags[]" <?php echo ($checked); ?> value="<?php echo $tags->term_id; ?>" onChange="this.form.submit()">
<span class="checkmark"></span>
<label><?php echo $tags->name; ?></label>
</label>
</li>
<?php
}
}
?>
</ul>
</form>
</div>
</div>
解决方案
推荐阅读
- r - 如何使用summary 和group_by?
- powershell - Export-csv 返回 1 个实例而不是整个结果
- javascript - 删除“编辑”文本单击“删除”文本
- amazon-web-services - AWS CLI:如何设置新复制实例的 VPC
- sql-server - 以整数值作为日期参数的 T-SQL 日期函数
- protocol-buffers - 多个原型中的 FileOptions 扩展
- ajax - 如何在 laravel 中使用 ajax 使用搜索栏?
- javascript - 简单立方体场景中的颗粒纹理
- google-apps-script - 我在 Regexp 获取表达式时遇到错误
- javascript - React - 单击第三个组件时更新多个组件的状态