按标签发布-创建重置

时间:2018-12-14 14:25:18

标签: php wordpress forms

我有一个相当整洁的过滤器标签,它将特定帖子类别中的结果缩小到只有该类别中具有所选标签的帖子。

我正在努力获得股票“选择标签...”选项以重置过滤器并再次显示所有结果-因此,一旦用户选择了标签,重置列表的唯一方法就是通过刷新。

这是我在function.php脚本中的过滤器:

// CASE STUDY FILTER 
function my_filters(){
    $args = array(
        'orderby' => 'date',
        'order' => $_POST['date']
    );

    if( isset( $_POST['tagfilter'] ) ):
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'id',
                'terms' => $_POST['tagfilter']
            )
        );

        $query = new WP_Query( $args );

        if( $query->have_posts() ):

            while( $query->have_posts() ):
                $query->the_post();

                echo "<article class=\"post-box " . get_post_class() . "\">";

                    echo "<a href=\"" . get_the_permalink() . "\" class=\"box-link\"></a>";

                    $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );

                    echo "<div class=\"cs-image\" style=\"background-image: url('" . $url . "')\"></div>";

                    $case_study = get_field('case_study_page_content');

                    if( $case_study ):

                        echo "<div class=\"cs-text-container\">";

                            while( have_rows('case_study_page_content') ): the_row();
                                $case_study_title = get_sub_field('title');
                                $case_study_author = get_sub_field('author');

                                echo "<h2>" . $case_study_title . "</h2>";

                                echo "<p>" . $case_study_author . "</p>";
                            endwhile;

                        echo "</div>";

                    endif;

                echo "</article>";


            endwhile;
            wp_reset_postdata();

        else :

            echo 'No case studies found';

        endif;

    die();

    endif;
}

add_action('wp_ajax_customfilter', 'my_filters');
add_action('wp_ajax_nopriv_customfilter', 'my_filters');
// END CASE STUDY FILTER

这是从这里获取输入信息的形式:

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="advanced-filterform">
    <?php
        if( $terms = get_terms( 'post_tag', 'orderby=name' ) ) :
            echo '<select class="tagfilter" name="tagfilter"><option>Select tag...</option>';
        foreach ( $terms as $term ) :
            echo '<option value="' . $term->term_id . '">' . $term->name . '</option>';
        endforeach;
            echo '</select>';
        endif;
    ?>

    <button>Apply filters</button>
    <input type="hidden" name="action" value="customfilter">
</form>

尽可能简单地在每个帖子上都不创建标签-我该如何实现。

0 个答案:

没有答案