首页 > 解决方案 > 类别和标签的 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>

标签: phpwordpress

解决方案


推荐阅读