使用Timber重构复杂的Wordpress模板

时间:2015-11-06 04:40:19

标签: php wordpress custom-post-type custom-taxonomy timber

我已经被重新设计并成为大学部门的Wordpress网站,我一直在尝试使用木材来使模板更清洁,更容易维护。

到目前为止,它一直很顺利,但我在将这个学位指南模板纳入控制器+视图设置方面遇到了很多麻烦。

学位和课程都有自定义帖子类型。还有一个自定义分类法,用于在课程指南中将课程分类为学期和年级。

我在下面的指南中包含了新生年代码,以及指向整个模板要点的链接。

我真的不确定从哪里开始。所以任何帮助,指针或资源都非常感谢!

  <?php if (is_single ( '19871' )) { 
      $freshmandegreeterms = 'freshman-year-bfa-filmmaking';
     $springfreshmandegreeterms = 'spring-freshman-year-bfa-filmmaking';
     $fallfreshmandegreeterms = 'fall-freshman-year-bfa-filmmaking';
      } elseif (is_single ( '19872' )) { 
      $freshmandegreeterms = 'freshman-year-bs-mis';
     $springfreshmandegreeterms = 'spring-freshman-year-bs-mis';
     $fallfreshmandegreeterms = 'fall-freshman-year-bs-mis';
      } elseif (is_single ( '19873' )) { 
      $freshmandegreeterms = 'freshman-year-bm-mis';
     $springfreshmandegreeterms = 'spring-freshman-year-bm-mis';
     $fallfreshmandegreeterms = 'fall-freshman-year-bm-mis';
      }
      elseif (is_single ( '19874' )) { 
      $freshmandegreeterms = 'freshman-year-ba-pop';
     $springfreshmandegreeterms = 'spring-freshman-year-ba-pop';
     $fallfreshmandegreeterms = 'fall-freshman-year-ba-pop';
      }
      ?>
    <h2>Freshman Year</h2>
    <div id="freshmanyear" class="row">
      <div id="freshmanfall"  class="medium-6 large-6 columns">
        <table>
          <tr>
            <td style="font-weight:bold;">Fall</td>
          </tr>
          <?php
  $fallfreshmanquery = new WP_Query( array ( 'post_type'      => 'course',
    'posts_per_page' => 20,
      'order' => ASC,
    'orderby' => 'menu_order',
     'tax_query' => array(
        array(
            'taxonomy' => 'degree-requirement',
            'terms' => $fallfreshmandegreeterms,
            'field' => 'slug'
        )
    ) ) );
while ( $fallfreshmanquery->have_posts() ) : $fallfreshmanquery->the_post();
?>
          <tr>
            <td><a href="#" data-reveal-id="myModal<?php the_ID(); ?>">
              <?php the_title(); ?>
              </a>
              <div id="myModal<?php the_ID(); ?>" class="reveal-modal" data-reveal>
              <?php the_content(); ?><br/>
                  <?php if ( get_post_meta( get_the_ID(), 'wpcf-credit-hours', true ) ) : ?>
                Credit Hours : <?php echo get_post_meta( get_the_ID(), 'wpcf-credit-hours', true ); ?>
                <?php endif; ?>

                <a class="close-reveal-modal">&#215;</a> </div></td>
          </tr>
          <?php endwhile;   ?>
        </table>
      </div>
      <!-- end freshman fall -->
      <div id="freshmanspring"  class="medium-6 large-6 columns">
        <table>
          <tr>
            <td style="font-weight:bold;">Spring</td>
          </tr>
          <?php
  $springfreshmanquery = new WP_Query( array ( 'post_type'      => 'course',
    'posts_per_page' => 20,
      'order' => ASC,
    'orderby' => 'menu_order',
     'tax_query' => array(
        array(
            'taxonomy' => 'degree-requirement',
            'terms' => $springfreshmandegreeterms,
            'field' => 'slug'
        )
    ) ) );
while ( $springfreshmanquery->have_posts() ) : $springfreshmanquery->the_post();
?>
          <tr>
            <td><a href="#" data-reveal-id="myModal<?php the_ID(); ?>">
              <?php the_title(); ?>
              </a>
              <div id="myModal<?php the_ID(); ?>" class="reveal-modal" data-reveal>
                <h2>
                  <?php the_title(); ?>
                </h2>
                <p>
                  <?php the_content(); ?><br/>
                  <?php if ( get_post_meta( get_the_ID(), 'wpcf-credit-hours', true ) ) : ?>
                Credit Hours : <?php echo get_post_meta( get_the_ID(), 'wpcf-credit-hours', true ); ?>
                <?php endif; ?>
                </p>
                <a class="close-reveal-modal">&#215;</a> </div></td>
          </tr>
          <?php endwhile;   ?>
        </table>
      </div>
      <!-- end freshman Spring --> 
    </div>
    <!-- end freshman year -->

以下是full gist的链接。

1 个答案:

答案 0 :(得分:0)

不是一个特定的问题,但这里有一些可能会有帮助的事情。

您可以为某些控制器逻辑创建一个wordpress自定义插件(一个类)。在这个插件中,它可以确定$ freshmandegreeterms值。

你可以对$ freshmandegreeterms的值进行排序的一种方法是创建一个接受参数的函数 - 即&#39; 19874&#39;。类似的东西:

public function set_freshman_terms( $code ) {
    $terms = [ 
       //Add necessary terms below
      '19871' => 'freshman-year-bfa-filmmaking',
      '19872' => 'freshman-year-bs-mis',
    ]

    $term = $terms[$code];

    $freshman           = [];
    $freshman['term']   = $term;
    $freshman['fall']   = 'fall-'.$term;
    $freshman['spring'] = 'spring-'.$term;

    //Set context
    $context['freshman'] = $freshman;
}

您甚至可以将该功能拉入更小的功能。但是,在您的视图中使用twig调用此数据会很好:

{{ freshman.term }}
{{ freshman.fall }}
{{ freshman.spring }}