WordPress PHP页面上的动态手风琴

时间:2016-03-24 15:22:12

标签: php jquery wordpress dynamic accordion

我在WordPress中有一个页面可以提取类别,然后列出该类别中的所有自定义帖子类型。

我试图对类别进行手风琴效果,以便所有帖子都不会一次显示。

这是PHP模板。

我尝试过多种解决方案,分页或手风琴无效。

<?php
/**
 *
 * Template Name: Online Courses Single Page
 *
 * The single post template. Used when a single post is queried.
 * 
 */

if(is_blog()){
    return load_template(THEME_DIR . "/template_blog.php");
}elseif(is_front_page()){
    return load_template(THEME_DIR . "/front-page.php");
}

$post_id = theme_get_queried_object_id();
$layout = theme_get_inherit_option($post_id, '_layout', 'general','layout');
$content_width = ($layout === 'full')? 960: 630;
get_header(); 
echo theme_generator('introduce',$post_id);?>
<div id="page">
    <div class="inner <?php if($layout=='right'):?>right_sidebar<?php endif;?><?php if($layout=='left'):?>left_sidebar<?php endif;?>">
        <?php echo theme_generator('breadcrumbs',$post_id);?>
        <div id="main">
        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
            <?php get_template_part('content','page'); ?>
<?php endwhile; // end of the loop.?>
<!-- Begin custom tax loop -->
  <?php
  //Retrieve custom taxonomy terms using get_terms and the custom post type.
    $categories = get_terms('course-category');
   //Iterate through each term
    foreach ( $categories as $category ) :
    ?>

      <div class="online-course-category">
           <h2><?php echo $category->name; ?></h2>
           <ul>
            <?php
           //Setup the query to retrieve the posts that exist under each term
            $posts = get_posts(array(
              'post_type' => 'online-courses',
              'posts_per_page'  => -1,
              'orderby' => 'name',
              'order' =>  'ASC',
              'taxonomy' => $category->taxonomy,
              'term'  => $category->slug,
              'nopaging' => true,
              ));
            // Here's the second, nested foreach loop that cycles through the posts associated with this category
            foreach($posts as $post) :
              setup_postdata($post); ////set up post data for use in the loop (enables the_title(), etc without specifying a post ID--as referenced in the stackoverflow link above)
            ?>

              <li class="online-course-list">
  <div id="online-course-container">
  <div id="online-course-icons">
                <?php if (get_field('course_has_power_points') == 'yes'): ?>
                    <?php if(get_field('course_has_power_points')) { echo '<span class="ppt-icon"><img src="http://americandreamreschool.com/wp-content/uploads/2015/09/1443334285_powerpoint2.png" /></span>'; } ?>
                <?php endif; ?>
                <?php if (get_field('course_has_pdfs') == 'yes'): ?>
                    <?php if(get_field('course_has_pdfs')) { echo '<span class="pdf-icon"><img src="http://williamstitle.com/wp-content/uploads/2016/01/1443385817_pdf.png" /></span>'; } ?>
                <?php endif; ?>
                <?php if (get_field('course_has_videos') == 'yes'): ?>
                    <?php if(get_field('course_has_videos')) { echo '<span class="video-icon"><img src="http://americandreamreschool.com/wp-content/uploads/2015/09/1443334308_24.TV_2.png" /></span>'; } ?>
                <?php endif; ?> 
                <?php if (get_field('course_has_audios') == 'yes'): ?>
                    <?php if(get_field('course_has_audios')) { echo '<span class="audio-icon"><img src="http://williamstitle.com/wp-content/uploads/2016/01/1443385658_volume-24.png" /></span>'; } ?>
                <?php endif; ?>
            </div>
    <?php if(get_field('course_title')) { echo '<div class="online-course-title"><h3>' . get_field('course_title') . '</h3></div>'; } ?>
        <div id="online-course-registration">
            <?php if(get_field('ce_credits_1')) { echo '<span class="ce-credits-1">' . get_field('ce_credits_1') . '</span> - '; } ?><?php if(get_field('price_1')) { echo '<span class="price-1">' . get_field('price_1') . '</span>'; } ?>
            <?php if(get_field('register_for_price_1_url')) { echo '<div class="online-course-register-button1"><a href="' . get_field('register_for_price_1_url') . '" target="_blank">Register</a></div>'; } ?>
            <?php if(get_field('ce_credits_2')) { echo '<span class="ce-credits-2">' . get_field('ce_credits_2') . '</span> - '; } ?><?php if(get_field('price_2')) { echo '<span class="price-2">' . get_field('price_2') . '</span>'; } ?>
            <?php if(get_field('register_for_price_2_url')) { echo '<div class="online-course-register-button2"><a href="' . get_field('register_for_price_2_url') . '" target="_blank">Register</a></div>'; } ?>
        </div>
    <?php if(get_field('course_description')) { echo '<div class="online-course-description">' . get_field('course_description') . '</div>'; } ?>
    <?php if(get_field('course_picture')) { echo '<div class="course-picture"><img src="' . get_field('course_picture') . '" /></div>'; } ?>
    <?php if(get_field('descriptive_video')) { echo '<div class="course-video">' . get_field('descriptive_video') . '</div>'; } ?>
    <?php if(get_field('disclaimer')) { echo '<div class="online-course-disclaimer">' . get_field('disclaimer') . '</div>'; } ?>
</div>
              </li>

            <?php endforeach; ?>

          </ul>
      </div><!-- .row -->     

  <?php endforeach; ?>
  <?php wp_reset_postdata(); ?>
            <div class="clearboth"></div>
        </div>
        <?php if($layout != 'full') get_sidebar(); ?>
        <div class="clearboth"></div>
    </div>
</div>
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:2)

看起来你错过了一些事情 - 除非他们在其他文件中。确保你已经获得了jQuery Accordion脚本 - wp_enqueue_script( 'jquery-ui-accordion' )并且你需要javascript

&#13;
&#13;
<script>
	jQuery(document).ready(function() {
	jQuery( '#accordion-container-id' ).accordion({
	heightStyle: 'content'
	});
	});
	</script>
&#13;
&#13;
&#13;

其中accordion-container-id是包装手风琴的元素的id。 (它可能适用于您的班级&#39;在线课程类别&#39;)。最后,我认为您需要针对<h2>标记更改{categeory名称周围的<h3>标记。这就是我如何把它全部付诸实践。