WP归档与自定义字段过滤器

时间:2014-12-09 14:08:49

标签: javascript php wordpress advanced-custom-fields

希望有人可以提供帮助。

我正在构建一个目录,并使用ACF来填充一些数据。我正在努力为我的一个ACF组中选择的选项构建过滤器。该字段组称为“成员目录”,我专门针对的字段是:'wha_service_offered'。

此处有四个复选框值:

  • 供应商:供应商
  • 制造商:制造商
  • 安装程序:安装程序
  • 顾问:顾问

我使用了复选框,因为可以向会员申请多个选项。

我已经使用以下内容设置了functions.php:

// Filter Services

add_action('pre_get_posts', 'my_pre_get_posts');

function my_pre_get_posts( $query )
{
// validate
if( is_admin() )
{
    return $query;
}

// allow the url to alter the query
// eg: http://www.website.com/members?wha_service_offered=installer
// eg: http://www.website.com/members?wha_service_offered=consultant
if( isset($_GET['wha_service_offered']) )
{
    $query->set('meta_key', 'wha_service_offered');
    $query->set('meta_value', $_GET['wha_service_offered']);
}   

// always return
return $query;

}    

在我的archive-members.php

PHP

<div id="search-services">
  <?php 
  $field = get_field_object('wha_service_offered');
  $values = isset($_GET['wha_service_offered']) ? explode(',', $_GET['wha_service_offered']) : array();
  ?>
  <ul>
    <?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
        <li>
            <input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
        </li>
    <?php endforeach; ?>
  </ul>
</div>

的javascript

<script type="text/javascript">
(function($) {

$('#search-services').on('change', 'input[type="checkbox"]', function(){

    // vars
    var $ul = $(this).closest('ul'),
        vals = [];

    $ul.find('input:checked').each(function(){

        vals.push( $(this).val() );

    });

    vals = vals.join(",");

    window.location.replace('<?php echo home_url('members'); ?>?wha_service_offered=' + vals);

    console.log( vals );

});

})(jQuery); 
</script>

使用过滤器时,它只返回一个空白页面。

如果有人能指出我犯了错误的地方,我将不胜感激。

感谢。

-

所以调试返回:

Declaration of Custom_Nav_Walker::start_el() should be compatible with 
Walker_Nav_Menu::start_el(&$output, $item, $depth = 0, $args = Array, $id = 0) in 
/Applications/MAMP/htdocs/website.com/wp-content/themes/wha/functions.php on line 169

Warning: Cannot modify header information - headers already sent by (output started at 
/Applications/MAMP/htdocs/website.com/wp-content/themes/wha/functions.php:169) in 
/Applications/MAMP/htdocs/website.com/wp-includes/pluggable.php on line 1179

违规项目是:

class Custom_Nav_Walker extends Walker_Nav_Menu {
function check_current($classes) {
return preg_match('/(current[-_])/', $classes);
}

function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$indent = ($depth) ? str_repeat("\t", $depth) : '';

$slug = sanitize_title($item->title);
$id = apply_filters('nav_menu_item_id', 'menu-' . $slug, $item, $args);
$id = strlen($id) ? '' . esc_attr( $id ) . '' : '';

$class_names = $value = '';
$classes = empty($item->classes) ? array() : (array) $item->classes;

$classes = array_filter($classes, array(&$this, 'check_current'));

if ($custom_classes = get_post_meta($item->ID, '_menu_item_classes', true)) {
  foreach ($custom_classes as $custom_class) {
    $classes[] = $custom_class;
  }
}

$class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
$class_names = $class_names ? ' class="' . $id . ' ' . esc_attr($class_names) . '"' : ' class="' . $id . '"';

$output .= $indent . '<li' . $class_names . '>';

$attributes  = ! empty($item->attr_title) ? ' title="'  . esc_attr($item->attr_title) .'"' : '';
$attributes .= ! empty($item->target)     ? ' target="' . esc_attr($item->target    ) .'"' : '';
$attributes .= ! empty($item->xfn)        ? ' rel="'    . esc_attr($item->xfn       ) .'"' : '';
$attributes .= ! empty($item->url)        ? ' href="'   . esc_attr($item->url       ) .'"' : '';

$item_output  = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;
$item_output .= '</a>';
$item_output .= $args->after;

$output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
}
}

我不明白为什么步行者导航会影响过滤器?

-

通过修改以下内容纠正第169行错误:

function start_el(&$output, $item, $depth, $args) { 

function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {}

我现在有以下错误:

Notice: Undefined index: choices in /Applications/MAMP/htdocs/website.com/wp-content/themes/wha/archive-members.php on line 54

Warning: Invalid argument supplied for foreach() in /Applications/MAMP/htdocs/website.com/wp-content/themes/wha/archive-members.php on line 54

涉及到:

<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>

0 个答案:

没有答案