WordPress:带过滤器的存档页面不起作用(ACF) [英] Wordpress: Archive page with Filter doesn't work (ACF)

查看:61
本文介绍了WordPress:带过滤器的存档页面不起作用(ACF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过ACF的复选框字段过滤我的自定义帖子类型。
我使用本教程: https:// www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

Im trying to filter my custom post types by a checkbox field of ACF. I work with this tutorial: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

现在,我得到了一个问题,当ich过滤自定义帖子类型的存档页面上的复选框。它只生成正确的URL,但不过滤帖子。

Now I got the problem that nothing change, when ich filter over the checkboxes on the archive page of the custom post type. It generates only the right URL but doesn't filter the posts.

有人知道为什么吗?

function.php:

function.php:

// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array( 
    'mitglieder'   => 'mitglieder'
);

// action
function my_pre_get_posts( $query ) {
    // bail early if is in admin
    if( is_admin() ) return;
    // bail early if not main query
    // - allows custom code / plugins to continue working
    if( !$query->is_main_query() ) return;
    // get meta query
    $meta_query = $query->get('meta_query');
    // loop over filters
    foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
        // continue if not found in url
        if( empty($_GET[ $name ]) ) {
            continue;   
        }


        // get the value for this filter
        // eg: http://www.website.com/events?city=melbourne,sydney
        $value = explode(',', $_GET[ $name ]);


        // append meta query
        $meta_query = array(
            array(
                'key'       => $name,
                'value'     => $value,
                'compare'   => 'IN',
            )
        );

    } 


    // update meta query
    $query->set('meta_query', $meta_query ); 
}
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);

register_taxonomy_for_object_type('category', 'projekte'); // Register Taxonomies for Category
$labels = array(
    'name' => __('Projekte', 'projekte'), // Rename these to suit
    'singular_name' => __('Projekt', 'projekte'),
    'add_new' => __('Projekt hinzufügen', 'projekte'),
    'add_new_item' => __('Neues Projekt hinzufügen', 'projekte'),
    'edit' => __('Bearbeiten', 'projekte'),
    'edit_item' => __('Projekt bearbeiten', 'projekte'),
    'new_item' => __('Neues Projekt', 'projekte'),
    'view' => __('Anschauen', 'projekte'),
    'view_item' => __('Projekt anschauen', 'projekte'),
    'search_items' => __('Projekte durchsuchen', 'projekte'),
    'not_found' => __('Projekt wurde nicht gefunden', 'projekte'),
    'not_found_in_trash' => __('Projekt wurde nicht im Papierkorb gefunden', 'projekte')
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'hierarchical' => true, // Allows your posts to behave like Hierarchy Pages
    'has_archive' => true,
    'supports' => array(
        'title',
        'excerpt'
    ), // Go to Dashboard Custom HTML5 Blank post for supports
    'can_export' => true, // Allows export in Tools > Export
    'taxonomies' => array(
        'category',
    ) // Add Category and Post Tags support
);
register_post_type('projekte', $args);

archive-projekte.php:

archive-projekte.php:

            <div id="archive-filters">
                <?php foreach( $GLOBALS['my_query_filters'] as $key => $name ): 
                    // get the field's settings without attempting to load a value
                    $field = get_field_object($key, false, false);
                    // set value if available
                    if( isset($_GET[ $name ]) ) {
                        $field['value'] = explode(',', $_GET[ $name ]);
                    }
                    // create filter
                    ?>
                    <div class="filter" data-filter="<?php echo $name; ?>">
                        <?php create_field( $field ); ?>
                    </div>
                <?php endforeach; ?>
            </div>

            <script type="text/javascript">
                (function($) {  
                    // change
                    $('#archive-filters').on('change', 'input[type="checkbox"]', function(){
                        // vars
                        var url = '<?php echo home_url('projekte'); ?>';
                            args = {};

                        // loop over filters
                        $('#archive-filters .filter').each(function(){
                            // vars
                            var filter = $(this).data('filter'),
                                vals = [];
                            // find checked inputs
                            $(this).find('input:checked').each(function(){
                                vals.push( $(this).val() );
                            });
                            // append to args
                            args[ filter ] = vals.join(',');
                        });
                        // update url
                        url += '?';
                        // loop over args
                        $.each(args, function( name, value ){
                            url += name + '=' + value + '&';
                        });
                        // remove last &
                        url = url.slice(0, -1);
                        // reload page
                        window.location.replace( url );
                    });

                    $('.button.acf-add-checkbox').parent().remove();
                })(jQuery);
            </script>


            <div class="projekt-archive">
                <?php
                    $args = array(
                        'post_type' => 'projekte',
                        'post_status' => 'publish',
                        'posts_per_page' => '-1'
                    );
                    $the_query = new WP_Query( $args );

                    if ( $the_query->have_posts() ) : ?>
                        <?php while ( $the_query->have_posts() ) : ?>
                       ......
                <?php 
                    endwhile; 
                  endif;
                ?>
                <?php wp_reset_query(); ?>


推荐答案

我使用了您的代码来尝试重新创建问题,遇到了许多问题,但还是奏​​效了。在您提供的视频教程的链接上,该操作与示例代码有所不同。

I used your code to try and recreate your issue and ran into a number of issues but got it working. On the link you supplied the video tutorial does things differently to the sample code.

我注意到的第一件事是,您正在函数中更改$ query,然后重新定义它在archive-projekte.php

The first thing I noticed is that you are changing the $query in the functions then redefining it in archive-projekte.php

$args = array(
      'post_type' => 'projekte',
      'post_status' => 'publish',
      'posts_per_page' => '-1'
);
$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) : 
   while ( $the_query->have_posts() ) : 
      //......
   endwhile; 
endif;

wp_reset_query();

您可以只使用标准循环的版本

you can just use a version of the standard loop instead

if ( have_posts() ) {  
   while ( have_posts() ) {
      the_post();         
      //.......
   }
}

其次,当我在Wordpress admin中将高级自定义字段(mitglieder)设置为复选框时,然后由过滤器div中的create_field()将其呈现为前端的复选框,但问题是该复选框保存在meta中数据作为序列化数据,因此它无法正常工作,因此我将高级自定义字段更改为单选按钮,一切正常。

Secondly when I set the advanced custom field(mitglieder) in Wordpress admin to be a checkbox it is then rendered as a checkbox on the front end by create_field() in the filter div but the problem is that checkboxes are saved in the meta data as serialized data so it didn't work so I changed the advanced custom field to a radio button it all works fine.

由此产生的新问题是过滤器div现在具有单选按钮。因此,我在$ field而不是create_field上使用foreach循环观看了视频教程和输出复选框,这意味着还需要更改javascript。

The new issue created by this is that the filter div now has radio buttons. So I watched the video tutorial and output checkboxes using a foreach loop on $field instead of using create_field, this means that the javascript needs to be changed also.

现在唯一问题仍然是,如果您需要将高级自定义字段设置为复选框,以便其中一个项目条目具有多个mitglieder值,那么您将需要使用序列化的元数据来使过滤器正常工作。

Now the only issue remains is if you need you advanced custom field to be checkbox sothat one of your projekte posts to have more than one mitglieder value then you would need to work with the serialized meta data in order to make the filter work correctly.

这类似于使用房屋和卧室的ACF示例视频,在这种情况下,房屋不能同时是2卧室房屋和3卧室房屋。

This works like ACF example video which uses houses and bedrooms and in that case a house cannot be a 2 bedroom house and a 3 bedroom house at the same time.

这篇关于WordPress:带过滤器的存档页面不起作用(ACF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆