根据表单中的复选框过滤帖子

时间:2018-04-05 19:35:39

标签: php wordpress html5 forms

我在WordPress自定义帖子类型存档页面上构建了一个表单,该页面使用从分类法中提取的值并使用选择多个字段进行设置来过滤所述帖子。表单提交到同一页面。它可能是“长途跋涉”但它的确有效。

然而,现在我被要求将选择多个字段更改为Checkboxes,我似乎无法正确使用它。表单提交时,不会传递选中的值。

这是我的代码,我尝试将两个选项中的一个转换为一组复选框。第一批只设置要拉入的值:

if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post();
        $topics = get_field('program_topic');
        foreach( $topics as $topic ): ?>
            <?php $program_topic_names[] = $topic->name ; ?>
            <?php $program_topic_links[] = $topic->slug ; ?>
            <?php $topic_name_results = array_unique($program_topic_names) ?>
            <?php $topic_link_results = array_unique($program_topic_links) ?>
        <?php endforeach;
        $types = get_field('program_type');
        foreach( $types as $type ): ?>
            <?php $program_type_names[] = $type->name ; ?>
            <?php $program_type_links[] = $type->slug ; ?>
            <?php $type_name_results = array_unique($program_type_names) ?>
            <?php $type_link_results = array_unique($program_type_links) ?>
        <?php endforeach;
endwhile; 
$final_topic_results = array_combine($topic_name_results, $topic_link_results);
$final_type_results = array_combine($type_name_results, $type_link_results);
endif; 

//print_r($final_topic_results);
//print_r($final_type_results);


wp_reset_postdata(); 

过滤行/表格:

<!--    Filter row  -->
    <div class="row filter_row">
    <div class="container">
        <div class="col span_3_of_12">
            <h2>Find a Program</h2>
        </div>
        <div class="col span_9_of_12">
            <form id='program_filters' class='program-filters multiple' method="post" onsubmit="showHide(); return false">
                <h4 class="span_12_of_12 no_padding" style="font-weight: 700;">Select one or more Topics and/or Types</h4>
                      <div class="styled-select multiple span_7_of_12">
                       <fieldset name="program_topic" id="program_topic">
                        <?php
                            foreach( $final_topic_results as $key => $value ):
                                echo "<input type='checkbox' ".checked( $_GET['program_topic'], $key )." value='$key' name='$key' /> $key <br>";
                            endforeach;
                        ?>
                        </fieldset>
                    </div>
                    <div class="styled-select span_3_of_12">
                        <select multiple name="program_type" id="program_type">
                            <?php
                                foreach( $final_type_results as $key => $value ):
                                echo "<option ".selected( $_GET['program_type'], $key )." value='$value'>$key</option>";
                            endforeach;
                            ?>
                        </select>
                    </div>
                   <div class="button_col span_2_of_12">
                   <input type='button' id='submitFormData' value='Find' onclick="this.form.submit()";>
                   <input type='button' value='Reset' onclick="window.location.href='/programs/'">
                    </div>
                </form>
        </div>
        <div style="clear:left;"></div>
    </div>
    </div>
    <!--    Filter row  -->

这是我以前没有包含的Post类型代码:

<!--    Program row     -->
<div class="container program-list-wrap"><ul>
<?php if ( have_posts() ) : ?>
      <?php while ( have_posts() ) : the_post(); ?>

<?php
$dateformatstring = "l, F d";
$unixtimestamp = strtotime(get_field('program_date'));
$instructor = get_field('program_instructor_name'); 
$instructorInfo = get_field('program_instructor_info');
$program_date = get_field('program_date');
$current = time(); ?>
    <?php if($current < $unixtimestamp || empty($program_date) ) : ?>
    <li><div class="program list">
    <div class="row program-intro">
    <div class="col span_9_of_12">
    <div class="program-vitals">
        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
    <?php if(get_field('program_date')) : ?><div class="program-date-time"><h4><?php echo date_i18n($dateformatstring, $unixtimestamp); if(get_field('program_time')) : ?> &mdash; <?php the_field('program_time');?><?php endif; ?></h4></div><?php endif; ?>
    <?php $types = get_field('program_type');
            if( $types ): ?><div class="types">
                <?php $i = 1; ?>
                <?php foreach( $types as $type ): ?>
                    <strong><a href="<?php echo get_term_link( $type ); ?>"><?php echo $type->name; ?></a></strong><?php $rowCount = count( get_field('program_type') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
                <?php endforeach; ?></div>
        <?php $topics = get_field('program_topic');
            if( $topics ): ?><div class="topics">
                <?php $i = 1; ?>
                <?php foreach( $topics as $topic ): ?>
                    <strong><a href="<?php echo get_term_link( $topic ); ?>"><?php echo $topic->name; ?></a></strong><?php $rowCount = count( get_field('program_topic') ); if($i < $rowCount): echo ', '; endif; $i++; ?>
                <?php endforeach; ?></div>
            <?php endif; ?>
        <?php if($instructor):?><strong>Instructor:</strong> <?php echo $instructor; if($instructorInfo):?>, <?php echo $instructorInfo; endif; endif; ?>
        <?php endif; ?></div>
    <?php if(get_field('program_intro')) : ?><div><?php the_field('program_intro'); ?></div><?php endif; ?>
        </div>
        <div class="col span_3_of_12">
        <?php $register = get_field('program_registration_url'); $registerAlum = get_field('program_alum_registration_url'); $cost = get_field('program_amount'); $brochure = get_field('program_brochure_url') ?>
            <div class="button_col"><h2><?php echo $cost ?></h2><a href="<?php echo $register ?>" class="action_button sml">Register Here</a><a href="<?php echo $registerAlum ?>" class="action_button sml">UMKC Alumni Registration</a> <?php if( $brochure) :?><a href="<?php echo $brochure ?>" class="action_button sml" target="_blank">Download a brochure</a><?php endif; ?></div>
            <a href="<?php the_permalink(); ?>" class="action_button">Learn More</a>
        </div>
        </div>
        <div class="program-extras">
            <?php if(get_field('program_includes')) : ?><div><h4><strong><?php the_title() ?> Includes:</strong></h4><?php the_field('program_includes'); ?></div><?php endif; ?>
    <?php if(get_field('program_learn')) : ?><div><h2>What You Will Learn</h2><?php the_field('program_learn'); ?></div><?php endif; ?>
    <?php if(get_field('program_key_topics')) : ?><div><h2>Key Topics</h2><?php the_field('program_key_topics'); ?></div><?php endif; ?>
    <?php if(get_field('program_attendee')) : ?><div><h2>Who Should Attend</h2><?php the_field('program_attendee'); ?></div><?php endif; ?>
    <?php if(get_field('program_instructor')) : ?><div><h2>About the Instructor</h2>
        <?php $image = get_field('program_instructor_photo');
        if( !empty($image) ): ?><div class="program_instructor_photo"><img src="<?php the_field('program_instructor_photo'); ?>);" /></div><?php endif; ?>
        <?php the_field('program_instructor'); ?></div>
    <?php endif; ?>
    <div style="clear: left;"></div>
    </div>

        </div></li>

        <?php endif; ?>
            <div style="clear: left;"></div>    
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endwhile; ?>
        <?php endif; ?>
    <div style="clear: left;"></div>
    </ul></div>
<!--    Program row     -->

我觉得我错过了一些明显的东西。任何帮助将不胜感激。

0 个答案:

没有答案