列出所有有共同分类的帖子作为当前分类!

时间:2019-07-22 作者:ErfanMHD

我有两种不同分类法类型的应用程序存档网站:卖家和种类,我想为每个卖家都有一个页面,在该页面内,我想为“种类”分类法的每个术语都有多个列表。

例如,如果我有以下两篇帖子:

Post 1 => "Seller: Apple" and "Kind: Action" 
Post 2 => "Seller: Apple" and "Kind: LifeStyle"
我想要一个卖“苹果”的页面,我想要两个盒子,分别是“行动”和“生活方式”(实际包含帖子的“种类”分类术语),我想要相关帖子显示在这些盒子中的任何一个。

你知道我该怎么做吗?

这是我的代码:

<?php //start by fetching the terms for the animal_cat taxonomy
$terms = get_terms( \'kind\', array(
    \'orderby\'    => \'count\',
    \'hide_empty\' => 0
) );
?>
<?php
// now run a query for each animal family
foreach( $terms as $term ) {

    // Define the query
    $args = array(
        \'post_type\' => \'appstore\',
        \'kind\' => $term->slug
    );
    $query = new WP_Query( $args );

    // output the term name in a heading tag                
    if( $query->have_posts() ){

       echo\'<h2>\' . $term->name . \'</h2>\';
    }
    // output the post titles in a list
    echo \'<ul>\';

        // Start the Loop
        while ( $query->have_posts() ) : $query->the_post(); ?>

        <li class="animal-listing" id="post-<?php the_ID(); ?>">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>

        <?php endwhile;

    echo \'</ul>\';

    // use reset postdata to restore orginal query
    wp_reset_postdata();

} ?>
它显示了所有贴子的类词:ios和所有贴子的类词:mac,无论我在查看哪个卖家页面。

我只需要在这段代码中添加额外的过滤布局。在“种类分类法”之前按“卖家分类法”限制应用程序。

我想从卖家gameloft存档页面中删除卖家apple Post的结果。在Gameloft归档页面内,我想通过iOS或mac将帖子分开。

屏幕截图:enter image description here

1 个回复
SO网友:Tom

没有定制的编程答案:

https://facetwp.com/ 将允许您按照所述设置过滤器。

自定义编程答案:

您可以创建一个短代码,用于呈现字段,供用户检查和提交。然后,将javascript提交处理程序附加到表单,以便当用户选中一两个框时,表单提交到ajax处理程序,并将选定的分类法交给自定义ajax函数。

自定义ajax函数将用户选择正确格式化为WordPress查询,运行循环并呈现HTML并将其传递回javascript。然后javascript将其加载到由您的短代码定义的容器中。

下面的代码块是我上面描述的一个快速而肮脏的示例。这是粗糙的,未经测试,需要抛光,但它会给你一个领先的开始。希望这有帮助。

<?php

//Do you want non-logged-in users to use this? Then you need the "nopriv" action below as well as standard wp_ajax
add_action( \'wp_ajax_nopriv_erfan_get_results\', \'wperfan_ajax\' );
add_action( \'wp_ajax_erfan_get_results\', \'wperfan_ajax\' );

function wperfan_ajax() {

    //Check security of ajax request
    check_ajax_referer( \'erfan_security\', \'my_security_param\' );

    $args = array();
    //Need security here.
    $custom_tax_a = ( isset( $_POST[\'fields\'][\'custom_tax_a\'] ) && is_array( $_POST[\'fields\'][\'custom_tax_a\'] ) ? $_POST[\'fields\'][\'custom_tax_a\'] : false );
    $custom_tax_b = ( isset( $_POST[\'fields\'][\'custom_tax_b\'] ) && is_array( $_POST[\'fields\'][\'custom_tax_b\'] ) ? $_POST[\'fields\'][\'custom_tax_b\'] : false );

    $tax_query = array();

    if ( $custom_tax_a ) {
        foreach ( $custom_tax_a as $slug => $tax_name ) {
            $tax_query[] = array(
                \'taxonomy\' => \'custom_tax_a\', //REPLACE
                \'field\'    => \'slug\',
                \'terms\'    => $slug,
            );
        }
    }

    if ( $custom_tax_b ) {
        foreach ( $custom_tax_b as $slug => $tax_name ) {
            $tax_query[] = array(
                \'taxonomy\' => \'custom_tax_b\', //REPLACE
                \'field\'    => \'slug\',
                \'terms\'    => $slug,
            );
        }
    }


    $args[\'tax_query\'] = array(
        \'relation\' => "AND",
        $custom_tax_a,
        $custom_tax_b,
    );

    $args[\'post_type\'] = \'MY_CUSTOM_POST_TYPE\'; // REPLACE
    $args[\'showposts\'] = \'10\'; // REPLACE

    //Add other args here... showposts, etc
    $query = new WP_Query( $args );

    $returndata = false;
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            // Need to customize your template here.
            // Also a better way to do this, but for answer purposes:
            $returndata .= sprintf( \'<article id="item-%s" class="post-result"><h2>%s</h2><div>%s</div></article>\', get_the_ID(), get_the_title(), get_the_content() );
        }
    }

    if ( $returndata ) {
        return json_encode( array( \'msg\' => \'filtered\', \'data\' => $returndata ) );
    }

    return json_encode( array( \'msg\' => \'no_filter\', \'data\' => $returndata ) );
}

add_shortcode( \'erfan_filters\', \'erfan_filters_shortcode\' );
function erfan_filters_shortcode() {
    $custom_tax_a = get_terms( \'custom_tax_a\', array( //REPLACE
        \'hide_empty\' => true,
    ) );

    $custom_tax_b = get_terms( \'custom_tax_b\', array( //REPLACE
        \'hide_empty\' => true,
    ) );

    ob_start(); ?>

    <div class="erfan-custom-filter">
        <form action="">
            <div class="filter-block custom-tax-one">
                <?php foreach ( $custom_tax_a as $ctone ) {
                    echo sprintf( \'<div>
                    <input type="checkbox" id="%s" name="%s" value="%s">
                    <label for="%s">%s</label>
                </div>\', $ctone->slug, \'custom_tax_a[]\', $ctone->slug, $ctone->slug, $ctone->name );
                } ?>
            </div>
            <div class="filter-block custom-tax-one">
                <?php foreach ( $custom_tax_b as $cttwo ) {
                    echo sprintf( \'<div>
                    <input type="checkbox" id="%s" name="%s" value="%s">
                    <label for="%s">%s</label>
                </div>\', $cttwo->slug, \'custom_tax_b[]\', $cttwo->slug, $cttwo->slug, $cttwo->name );
                } ?>
            </div>

            <?php wp_nonce_field( \'erfan_security\', \'my_security_param\', true, true ); ?>
            <input type="submit" value="Submit"/>
        </form>
    </div>

    <div class="erfan-results-container">
        <div class="erfan-results">
            <?php // The Custom Posts found will go here...
            ?>
        </div>
    </div>

    <?php
    echo ob_get_clean();
}

add_action( \'wp_footer\', \'wperfan_footer_script\' );
function wperfan_footer_script() {
    ob_start();
    ?>

    <script type="text/javascript">
        /**
         * jQuery serializeObject
         * @copyright 2014, macek <[email protected]>
         * @link https://github.com/macek/jquery-serialize-object
         * @license BSD
         * @version 2.5.0
         */
        !function (e, i) {
            if ("function" == typeof define && define.amd) define(["exports", "jquery"], function (e, r) {
                return i(e, r)
            }); else if ("undefined" != typeof exports) {
                var r = require("jquery");
                i(exports, r)
            } else i(e, e.jQuery || e.Zepto || e.ender || e.$)
        }(this, function (e, i) {
            function r(e, r) {
                function n(e, i, r) {
                    return e[i] = r, e
                }

                function a(e, i) {
                    for (var r, a = e.match(t.key); void 0 !== (r = a.pop());) if (t.push.test(r)) {
                        var u = s(e.replace(/\\[\\]$/, ""));
                        i = n([], u, i)
                    } else t.fixed.test(r) ? i = n([], r, i) : t.named.test(r) && (i = n({}, r, i));
                    return i
                }

                function s(e) {
                    return void 0 === h[e] && (h[e] = 0), h[e]++
                }

                function u(e) {
                    switch (i(\'[name="\' + e.name + \'"]\', r).attr("type")) {
                        case"checkbox":
                            return "on" === e.value ? !0 : e.value;
                        default:
                            return e.value
                    }
                }

                function f(i) {
                    if (!t.validate.test(i.name)) return this;
                    var r = a(i.name, u(i));
                    return l = e.extend(!0, l, r), this
                }

                function d(i) {
                    if (!e.isArray(i)) throw new Error("formSerializer.addPairs expects an Array");
                    for (var r = 0, t = i.length; t > r; r++) this.addPair(i[r]);
                    return this
                }

                function o() {
                    return l
                }

                function c() {
                    return JSON.stringify(o())
                }

                var l = {}, h = {};
                this.addPair = f, this.addPairs = d, this.serialize = o, this.serializeJSON = c
            }

            var t = {
                validate: /^[a-z_][a-z0-9_]*(?:\\[(?:\\d*|[a-z0-9_]+)\\])*$/i,
                key: /[a-z0-9_]+|(?=\\[\\])/gi,
                push: /^$/,
                fixed: /^\\d+$/,
                named: /^[a-z0-9_]+$/i
            };
            return r.patterns = t, r.serializeObject = function () {
                return new r(i, this).addPairs(this.serializeArray()).serialize()
            }, r.serializeJSON = function () {
                return new r(i, this).addPairs(this.serializeArray()).serializeJSON()
            }, "undefined" != typeof i.fn && (i.fn.serializeObject = r.serializeObject, i.fn.serializeJSON = r.serializeJSON), e.FormSerializer = r, r
        });

        window.wperfan_filter = (function (window, document, $, undefined) {
            \'use strict\';
            var app = {};

            app.init = function () {

                app.the_form = $(\'.erfan-custom-filter form\');
                the_form.on(\'submit\', app.process_form);

            };


            app.process_form = function (e) {
                e.preventDefault();

                var data = $(this).serializeObject();

                var $filter_call = $.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                        \'action\': \'erfan_get_results\',
                        \'security\': data.my_security_param
                        \'fields\': data
                    }
                });

                $.when($filter_call).then(
                    function (response) {
                        if (response.msg === \'filtered\') {
                            console.log(\'Filter worked\');
                        }
                        if (response.msg === \'no_filter\') {
                            console.log(\'Default results, no filter.\');
                        }
                        $(\'.erfan-results\').html(response.data);
                    },
                    function (error) {
                        console.log(\'ERROR\');
                        console.log(error);
                        alert(\'An error occured on the server.\') //REPLACE
                    });
                return false;
            };

            $(document).ready(function () {
                if ($(\'.erfan-custom-filter\').length > 0) {
                    app.init();

                    //If you want the form to load data on page load, uncomment the following:
                    // app.the_form.trigger(\'submit\');
                }
            });
        })(window, document, jQuery);

    </script>

    <?php
    echo ob_get_clean();
}

资源:

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post