久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用下拉菜單動態過濾 Wordpress 帖子(使用 php 和

Dynamically filter Wordpress posts with dropdown menu (using php and ajax)(使用下拉菜單動態過濾 Wordpress 帖子(使用 php 和 ajax))
本文介紹了使用下拉菜單動態過濾 Wordpress 帖子(使用 php 和 ajax)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

目標:我想制作一個動態頁面,允許訪問者從下拉菜單中選擇月份和年份,并根據所選值更改頁面上的內容(帖子).

Goal: I would like to make a dynamic page that allows the visitor to select a month and year from a drop-down menu and have the content (the posts) on the page change according the the values selected.

我目前正在使用以下代碼來顯示來自特定月份和年份的特定類別的帖子.

I'm currently using the following code to display posts from a particular category from a particular month and year.

<?php query_posts("cat=3&monthnum=12&year=2011"); ?> <?php if (have_posts()) : ?>
     <ul>
    <?php while (have_posts()) : the_post(); ?>
        <li>
           <?php the_title(); ?>
           <?php the_excerpt(); ?>
        </li>
    <?php endwhile; ?>
     </ul><?php endif; ?>

效果很好,但我想讓頁面動態化,以便訪問者可以從下拉菜單中選擇月份和年份,并根據所選值更改內容.我已經在這里發布了它如何工作的圖片:fivepotato.com/images/ex1.png和 Fivepotato.com/images/ex2.png.

It works well, but I would like to make the page dynamic so that the visitor can select a month and year from a drop-down menu and have the content change according the the values selected. I've posted pictures of how it would work here: fivepotato.com/images/ex1.png and fivepotato.com/images/ex2.png.

要完成這項工作,我知道我必須將 monthnum 的值設為變量(取自下拉列表:

To make this work, I know that I will have to make the value of monthnum a variable (which is taken from the dropdown list:

<?php $monthvar = $_POST["month"]; query_posts("cat=3&monthnum=$monthvar&year=2011");?>

我對 Ajax 沒有太多經驗,但我認為我需要使用它來使內容重新過濾,每月從下拉菜單中選擇一次.

I don't have much experience with Ajax, but I assume that I will need to use it to make the content re-filter once a month is selected from the dropdown menu.

我在以下網站上發現了類似的查詢:askthecssguy.com/2009/03/checkbox_filters_with_jquery_1.html

I have found similar inquires on the following site: askthecssguy.com/2009/03/checkbox_filters_with_jquery_1.html

我發現了一個類似于我想做的工作示例:http://www.babycarers.com/search?ajax=0&searchref=37609&start=0&lat=&lon=&city=&radius=0&spec1=1&spec2=1&spec3=1&spec4=1&spec5=1&spec6=1&spec7=1&inst1=1&inst2=1&inst3=1&inst4=1&inst5=1&inst6=1&inst7=1&;minfee=any&maxfee=any&av1=1&keywords=&country=CA&sort=fee&resultsperpage=10

And I have found a working example similar to what I would like to do at: http://www.babycarers.com/search?ajax=0&searchref=37609&start=0&lat=&lon=&city=&radius=0&spec1=1&spec2=1&spec3=1&spec4=1&spec5=1&spec6=1&spec7=1&inst1=1&inst2=1&inst3=1&inst4=1&inst5=1&inst6=1&inst7=1&minfee=any&maxfee=any&av1=1&keywords=&country=CA&sort=fee&resultsperpage=10

如果有人可以幫助我解決實現此問題所需的 javascript/Ajax,我將不勝感激.

If anyone could help me out with the javascript/Ajax necessary to pull this off I would be greatly appreciative.

推薦答案

幾乎 1000 次查看,沒有一條評論.好吧,我也需要這個并決定制作它.我已經在下面分享了 JavaScript 和 Wordpress 代碼,供未來的人們使用.它看起來很多,但那是因為我已經定義了一些 jQuery 函數,您可以稍后將其與 .extend 一起使用.它所做的只是尋找具有 CSS 類 .content-filterselect 元素(下拉列表).

Almost 1000 views and not a single comment. Well, I also needed this and decided to make it. I've shared the JavaScript and Wordpress code below for people in the distant future to use. It looks like a lot, but that's because I've defined some jQuery functions you can use later with .extend. All it's doing is looking for a select element (a dropdown) with CSS class .content-filter.

一旦找到,它就會使用下拉列表的 id 將 GET 變量設置為當前選擇的值,然后重定向到相同的 URL 并添加這些 GET 變量.例如,如果下拉列表的 id 是 product_filter,并且它的值設置為 date,那么它將設置 GET 變量 product_filter=date.這很棒,因為它不關心您的 Wordpess 詳細信息 - 它只關心 select 元素.

Once found, it uses the dropdown's id to set a GET variable to the value currently selected, then it redirects to this the same URL and adds these GET variables. For example, if the id of the dropdown was product_filter, and this had a value set to date, then it would set the GET variable product_filter=date. It's great because it doesn't care about your Wordpess details - all it cares about is the select element.

// A bunch of helper methods for reading GET variables etc from the URL
jQuery.extend({
    urlGetVars : function() {
        var GET = {};
        var tempGET = location.search;
        tempGET = tempGET.replace('?', '').split('&');
        for(var i in tempGET) {
            var someVar = tempGET[i].split('=');
            if (someVar.length == 2) {
                GET[someVar[0]] = someVar[1];
            }
        }
        return GET;
    },
    urlGetVar : function(name) {
        return $.urlGetVars()[name];
    },
    serializeUrlVars : function(obj) {
        var str = [];
        for(var p in obj)
         str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    currentUrl : function() {
        return window.location.href.slice(0,window.location.href.indexOf('?'));
    }
});

// Adds functionality to filter content using a dropdown
var ContentFilter = function ($) {
    $(document).ready(function() {
        // Return to a scroll position if exists
        var scroll = $.urlGetVar('scroll');
        if (typeof scroll != 'undefined') {
            $(window).scrollTop(scroll);
        }
        // Prepare the filter dropdowns
        $('.content-filter').each(function(){
            var me = $(this);
            // e.g. content-filter-product
            var id = me.attr('id');
            // Refresh with selected filter on change
            var refresh = function() {
                var GET = $.urlGetVars();
                GET[id] = me.val();
                // Save scroll position, return to this position on load
                GET['scroll'] = $(window).scrollTop();
                var newVar = $.currentUrl() + '?' + $.serializeUrlVars(GET);
                window.location = newVar;
            };
            me.change(refresh);
        });
    });
}(jQuery);

現在是 Wordpress 代碼.我們真正需要的是生成具有某種 id 的 select 并將類設置為 .content-filter.此代碼要求提供類似post"或product"的帖子類型,并生成 select 元素.為方便起見,它然后返回 GET 變量,如果沒有設置,則默認為最新".請注意,$fields 數組設置了所有不同的orderby 值喜歡支持.根據您的類型,您始終可以使用 $_GET['product_filter']$_GET['post_filter'] 在模板中的任何位置訪問它.這意味著在任何給定頁面上只能存在一個,但您需要這樣做 - 否則 jQuery 將不知道該使用哪個.您可以擴展此代碼以設置自定義 ID 或您以后喜歡的任何內容.

Now the Wordpress code. All we really need is to generate the select with some kind of id and set the class to .content-filter. This code asks for a post type like 'post' or 'product' and makes the select element. It then returns the GET variable for convenience, and if none is set then it defaults to 'newest'. Notice that the $fields array sets all the different orderby values you'd like to support. You can always access it anywhere in the template with $_GET['product_filter'] or $_GET['post_filter'] depending on what your type is. This means that only one can exist on any given page, but you want that - otherwise jQuery won't know which to use. You can extend this code to set a custom id or anything you like later.

function ak_content_filter($post_type_id = 'post', &$filter_get_value, $echo = TRUE) {
    $dropdown = '<div class="content-filter-wrapper">';
    // The dropdown filter id for this post type
    $filter_id = $post_type_id.'_filter';
    // The actual dropdown
    $dropdown .= '<label for="'. $filter_id .'">Filter</label><select id="'. $filter_id .'" class="content-filter" name="'. $filter_id .'">';
    // The available ways of filtering, to sort you'd need to set that in the WP_Query later
    $fields = array('date' => 'Newest', 'comment_count' => 'Most Popular', 'rand' => 'Random');
    $filter_get_value = isset($_GET[$filter_id]) ? $_GET[$filter_id] : 'newest'; // default is 'newest'
    foreach ($fields as $field_value=>$field_name) {
        $dropdown .= '<option value="'. $field_value .'" '. selected($field_value, $filter_get_value, FALSE) .'>'. $field_name .'</option>';
    }
    $dropdown .= '</select></div>';
    // Print or return
    if ($echo) {
        echo $dropdown;
    } else {
        return $dropdown;
    }
}

現在是有趣的部分 - 將其放在內容頁面中.我們所有的工作都會通過一些甜蜜而簡短的代碼得到回報:

Now the fun part - putting it together in the content page. All our work pays off with some sweet and short code:

// This will fill $product_filter with $_GET['product_filter'] or 'newest' if it doesn't exist
ak_content_filter('product', $product_filter);
$args = array('post_type' => 'product', 'orderby' => $product_filter);
// This is just an example, you can use get_pages or whatever supports orderby
$loop = new WP_Query( $args );

// OR, to avoid printing:
$dropdown = ak_content_filter('product', $product_filter, FALSE);
// ... some code ...
echo $dropdown;

我使用了自定義帖子類型產品",但如果您使用的是帖子",只需將其替換即可.如果他們還沒有的話,有人可能應該把它變成一個插件:P

I used the custom post type 'product', but if you're using 'post' just replace that. Someone should probably make this into a plugin if they haven't already :P

這篇關于使用下拉菜單動態過濾 Wordpress 帖子(使用 php 和 ajax)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 天天干天天操天天看 | 青青草在线视频免费观看 | 亚洲精品乱码久久久久久黑人 | 亚洲成人av在线播放 | 亚洲精品一二区 | 日本成人在线免费视频 | 日韩视频一级 | 五月综合激情网 | 涩涩99| 日本不卡免费新一二三区 | 人人爽人人爽 | 国产一区欧美 | 国产高清免费 | 国产精品美女www | 四虎网站在线观看 | 二区不卡 | 91大神在线资源观看无广告 | 9999国产精品欧美久久久久久 | 国产98色在线 | 日韩 | 99精品在线免费观看 | 91久久精品国产 | 丁香婷婷久久久综合精品国产 | 日韩精品一区二区三区视频播放 | tube国产 | 国产免费一区二区三区 | 天天操天天玩 | 国产欧美日韩视频 | 成年无码av片在线 | 精品国产网 | 午夜噜噜噜 | 99久久婷婷国产综合精品电影 | 一级做a| 操久久 | 免费能直接在线观看黄的视频 | 欧美成人一区二区三区片免费 | 99精品久久 | 免费在线色 | 久久精品伊人 | 欧美日韩一| 久久精品视频网站 | www在线视频 |