如何同时使用php和js过滤内容

时间:2019-02-11 11:36:30

标签: javascript php wordpress

我正在设置一个过滤器栏,以根据用户选择的内容过滤WordPress帖子。

当前代码为每个帖子类别提供反映我所针对标准的帖子。假设某条帖子已于2019年2月发布。该帖子的标题为“ FEB”,“ 2019”,其标题为“ _”,而不是空格作为附加类。

页面加载后,URL中带有参数,所有文章都会消失,然后仅显示具有与$ _GET参数匹配的类的文章。

我在代码以及回显语句中使用JS。

由于某种原因,它无法正常工作,如果有人可以指出我的问题或给我更好的替代此长代码的方法,那就太好了。

谢谢!

function filterbar(){

    $current_ID =$_GET['page_id'];

    echo "
        <div class='filterbar containerfluid'>
            <div class='row'>
                <div class='col-12'>";

                    if (get_locale() == 'en_US') {
                        echo "<form action='#' method='get'>
                        <div class='dropdown'>
                            <select name='month'>
                              <option value='all'>By Month</option>
                              <option value='Jan'>January</option>
                              <option value='Feb'>February</option>
                              <option value='Mar'>March</option>
                              <option value='Apr'>April</option>
                              <option value='May'>May</option>
                              <option value='Jun'>June</option>
                              <option value='Jul'>July</option>
                              <option value='Aug'>August</option>
                              <option value='Sep'>September</option>
                              <option value='Oct'>October</option>
                              <option value='Nov'>November</option>
                              <option value='Dec'>December</option>
                            </select>
                         </div>
                         <div class='dropdown'>
                          <select name='year'>
                            <option value='all'>By Year</option>
                            <option value='2018'>2018</option>
                            <option value='2019'>2019</option>
                          </select>
                         </div>
                         <div class='dropdown'>
                          <select name='category'>
                            <option value='all'>All</option>
                            <option value='Excise Tax'>Excise Tax</option>
                            <option value='Corporate Income Tax'>Corporate Income Tax</option>
                            <option value='Withholding Tax'>Withholding Tax</option>
                            <option value='Tax Treaties'>Tax Treaties</option>
                          </select>
                         </div>

                         <input type='text' name='page_id' value=' $current_ID ' style='display: none;'>

                         <input type='text' name='string' value='all' placeholder='Enter keywords'>
                         <input type='submit' value='Filter' class='button'></input>

                     </form>";
                    } else {
                        echo "<form action='#' method='get'>
                        <div class='dropdown'>
                            <select name='month'>
                              <option value='all'>شهريا</option>
                              <option value='Jan'>January</option>
                              <option value='Feb'>February</option>
                              <option value='Mar'>March</option>
                              <option value='Apr'>April</option>
                              <option value='May'>May</option>
                              <option value='Jun'>June</option>
                              <option value='Jul'>July</option>
                              <option value='Aug'>August</option>
                              <option value='Sep'>September</option>
                              <option value='Oct'>October</option>
                              <option value='Nov'>November</option>
                              <option value='Dec'>December</option>
                            </select>
                         </div>
                         <div class='dropdown'>
                          <select name='year'>
                            <option value='all'>سنويا</option>
                            <option value='2018'>2018</option>
                            <option value='2019'>2019</option>
                          </select>
                         </div>
                         <div class='dropdown'>
                          <select name='category'>
                            <option value='all'>بالتصنيف</option>
                            <option value='Excise Tax'>Excise Tax</option>
                            <option value='Corporate Income Tax'>Corporate Income Tax</option>
                            <option value='Withholding Tax'>Withholding Tax</option>
                            <option value='Tax Treaties'>Tax Treaties</option>
                          </select>
                         </div>

                         <input type='text' name='page_id' value=' $current_ID ' style='display: none;'>

                         <input type='text' name='string' value='الكل ' placeholder='Enter keywords'>
                         <input type='submit' value='منقي' class='button'></input>

                     </form>";
                    }



                echo "
            </div>
            </div>
            </div>
    ";

        if(isset($_GET['year']) || isset($_GET['month']) || isset($_GET['string']) || isset($_GET['category']) ){

        if($_GET['year'] != 'all' || $_GET['month'] != 'all' || $_GET['string'] != 'all' || $_GET['category'] != 'all' ){
            echo "
            <script>

                jQuery(document).ready(function($){

                    $( 'article.pressrelease' ).hide();

                });

            </script>

            ";
        }

        if($_GET['year'] != 'all'){

        $filteryear = $_GET['year'];
        echo "
        <script>

        jQuery(document).ready(function($){


        if($( 'article.pressrelease' ).hasClass(" . $filteryear . ")){
            $( 'article.pressrelease' ).show();
        }

        });

        </script>
        ";
    }

        if($_GET['month'] != 'all'){
            $filtermonth = $_GET['month'];
                    echo "
                    <script>

                    jQuery(document).ready(function($){


                    if($( 'article.pressrelease' ).hasClass(" . $filtermonth . ")){
                        $( 'article.pressrelease' ).show();
                    }

                    });

                    </script>
                    ";
            }

    } } add_shortcode( 'filterbarList', 'filterbar' );

1 个答案:

答案 0 :(得分:0)

在不知道您遇到的确切问题的情况下,最明显的事情是您仅需要引用要显示的文章而不是全部文章。

所以改变

$( 'article.pressrelease' ).show();

$(." . $filteryear . ").show();

另外,qoute之前的第一个时间段应该在那里。