自定义Wordpress下拉菜单可从自定义帖子类型中提取自定义分类,并隐藏不匹配的分类

时间:2018-10-02 10:51:14

标签: javascript jquery wordpress custom-post-type

我有一个登录页面,其中显示来自自定义帖子类型的所有帖子。我创建了下拉过滤器,用于引入自定义字段和分类法。我想隐藏所有与所选下拉值不匹配的帖子。

该代码先前已在我的网站上编码,并且可以工作,但是我尝试复制第二种帖子类型的代码,但无法正常工作。下拉列表将填充正确的分类法值,但是,当选择任何下拉列表时,它们将不执行任何操作……不进行过滤,不执行任何操作。它们确实可以使用原始代码,但不能使用我复制的代码(请注意,我复制了所有内容,只是更改了帖子类型名称。我认为问题出在JS代码某处,但我不确定。

我在Wordpress functions.php中设置了带有自定义分类法的自定义帖子类型:

<li>
  <input type="checkbox" id="f6c9677c-309c-446c-9f78-757467ec7adc">
  <label for="f6c9677c-309c-446c-9f78-757467ec7adc">
    <span> Testigos de la Valoracion </span>
    <button disabled>Button</button>
  </label>
</li>

然后我注册自定义分类法:

//Charter Content East Type
$charter_labels = array(
    'name' => _x('Charter Aircraft East', 'post type general name'),
    'singular_name' => _x('Charter Aircraft East', 'post type singular name'),
    'add_new' => _x('Add New', 'news-post'),
    'add_new_item' => __('Add New Charter Aircraft East'),
    'edit_item' => __('Edit Charter Aircraft East'),
    'new_item' => __('New Charter Aircraft East'),
    'view_item' => __('View Charter Aircraft East'),
    'search_items' => __('Search Charter Aircraft East'),
    'not_found' =>  __('No Charter Aircraft found East'),
    'not_found_in_trash' => __('No Charter Aircraft East found in Trash'), 
    'menu_name' => 'Charter Fleet East'
);

$charter_args = array(
    'labels' => $charter_labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true, 
    'menu_position' => 5,
    'menu_icon' => '/wp-content/themes/avjet/style/img/airplane-admin-icon.png',
    'query_var' => true,
    'rewrite' => array('slug'=>'private-jet-charter-fleet-east', 'with_front'=>false, 'feeds'=>true, 'pages'=>true),
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'supports' => array('title','editor','author','trackbacks','custom-fields','revisions','thumbnail','page-attributes')
);
register_post_type( 'charter-east' , $charter_args );

一切正常。

然后我在前端显示列表:

$c_manufacturer3_labels = array(
    'name' => _x( 'Manufacturer / Model', 'taxonomy general name' ),
    'singular_name' => _x( 'Manufacturer / Model', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Manufacturers' ),
    'all_items' => __( 'All Manufacturers' ),
    'edit_item' => __( 'Edit Manufacturer / Model' ), 
    'update_item' => __( 'Update Manufacturer / Model' ),
    'add_new_item' => __( 'Add New Manufacturer / Model' ),
    'new_item_name' => __( 'New Manufacturer / Model' ),
    'menu_name' => __( 'Manufacturers' ),
    );

$c_manufacturer3_args = array(
    'hierarchical' => true,  
    'labels' => $c_manufacturer3_labels, 
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'charter-manufacturers3', 'with_front' => true)
    );
register_taxonomy('charter-manufacturers3', array('charter-east'), $c_manufacturer3_args);

这也可以正常工作(请注意,清单被拉到“组”中,这是另一种自定义分类法,与“制造商”的注册方式相同)。

最后,如果制造商正确匹配,那么我有一些JS应该在选择结果时过滤结果。选择后不执行任何操作(控制台中没有错误消息)。

    <div id="pagetext">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php the_content(); ?>
      <?php endwhile; endif; ?>

      <div class="sorting">
        <label>Sort Results 2:</label>

        <?php 
        //Get the search variables, if they exist
        $passengers = -1;
        $manufacturer = -1;
        $model = -1;
        $group = -1;

        if ($_GET['passengers']) { 
            $passengers = $_GET['passengers'];
        }

        if ($_GET['manufacturer']) { 
            $manufacturer = $_GET['manufacturer'];
        }

        if ($_GET['model']) { 
            $model = $_GET['model'];
        }

        if ($_GET['group']) { 
            $group = $_GET['group'];
        }


        //Get the maximum Number of Passengers to create the proper drop-down options
        $max = 4;
        $charter_args = array(
            'post_type' => 'charter-east',
            'nopaging' => true,
            'orderby' => 'menu_order',
            'order' => 'ASC',
         ); 
        $charter_query = new WP_Query($charter_args);
        if ($charter_query->have_posts()) : while ($charter_query->have_posts()) : $charter_query->the_post();
            if (get_field('number_of_passengers') > $max) {
                $max = get_field('number_of_passengers');
            }
        endwhile; endif;
        ?>
        <select id="passengers">
          <option value="-1">Number of Passengers</option>
          <?php for ($i=4; $i <= $max; $i++) { ?>
            <option value="<?php echo $i; ?>" <?php if ($passengers == $i) { echo 'selected="selected"'; } ?>><?php echo $i; ?></option>
          <?php } ?>
        </select>
        <?php wp_reset_query(); ?>

        <?php wp_dropdown_categories('show_option_none=Aircraft Manufacturer&taxonomy=charter-manufacturers3&orderby=name&name=manufacturer&id=manufacturer&hierarchical=1&depth=1&hide_empty=1&selected='.$manufacturer); ?>

        <div id="modeldrop">
          <select id="model" disabled="disabled">
            <option value="-1">Aircraft Model</option>
          </select>
        </div>

        <?php wp_dropdown_categories('show_option_none=Aircraft Category&taxonomy=charter-groups&orderby=name&name=group&id=group&hierarchical=1&depth=1&hide_empty=1&selected='.$group); ?>

        <?php 
        //Create a drop-down for each of the Manufacturer's Sub-Categories (Models)
        $catargs = array(
            'taxonomy'     => 'charter-manufacturers3',
            'hierarchical' => 1,
            'depth' => 1,
            'hide_empty' => 1
        );

        $categories = get_categories($catargs);

        foreach ($categories as $cat) { 

            $dropargs = array(
                'show_option_none' => 'Aircraft Model',
                'taxonomy' => 'charter-manufacturers3',
                'orderby' => 'name',
                'child_of' => $cat->term_id,
                'hide_if_empty' => 1,               
            );
            if ($cat->term_id == $manufacturer) {
                $dropargs['selected'] = $model;
            }
            echo '<div id="cat-'.$cat->term_id.'" class="hidden">';
            wp_dropdown_categories($dropargs);      
            echo '</div>';      
        }
        ?>
      </div>
 <?php      
    $_terms = get_terms( array('charter-groups') );

    foreach ($_terms as $term) :

        $term_slug = $term->slug;
        $_posts = new WP_Query( array(
                    'post_type'         => 'charter-east',
                    'posts_per_page'    => 2000, //important for a PHP memory limit warning
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'charter-groups',
                            'field'    => 'slug',
                            'terms'    => $term_slug,
                        ),
                    ),
                ));

        if( $_posts->have_posts() ) :

            echo '<section class="groups" style="clear:both;">';
            echo '<h2 class="group-name">'. $term->name .'</h3>';

            while ( $_posts->have_posts() ) : $_posts->the_post();
            ?>
                <div class="listing charter" manufacturers="<?php foreach((get_the_terms($post->ID, 'charter-manufacturers3')) as $m) { echo $m->term_id.' '; } ?>" passengers="<?php the_field('number_of_passengers'); ?>" groups="<?php foreach((get_the_terms($post->ID, 'charter-groups')) as $g) { echo $g->term_id.' '; } ?>">
            <a class="aircraft-thumb" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('aircraft-slideshow-large'); ?></a>
            <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
            <ul>
              <li><?php the_field('bullet_1'); ?></li>
              <li><?php the_field('bullet_2'); ?></li>
              <li><?php the_field('bullet_3'); ?></li>
              <?php if (get_field('bullet_4')) { ?>
                <li><?php the_field('bullet_4'); ?></li>
              <?php } ?>
              <?php if (get_field('bullet_5')) { ?>
                <li><?php the_field('bullet_5'); ?></li>
              <?php } ?>
            </ul>
          </div>
            <?php
            endwhile;

           echo '</section>';

        endif;
        wp_reset_postdata();

    endforeach;
?>

对于其他一些下拉列表,JS还有一些其他代码,它们都是自定义分类法或通过Advanced Custom Fields Pro添加到Post类型的自定义字段。除了下拉菜单中隐藏未选择的列表之外,所有部分都起作用。

0 个答案:

没有答案