WordPress自定义帖子类型模板+条件语句:任何简化方法


WordPress Custom Post Type Template + Conditional Statements: Any Way to Simplify

所以我仍然掌握WordPress自定义帖子类型的窍门以及如何将它们集成到现有模板中,但我有一个关于我想出的代码是否可以简化的快速问题?

我需要从一个特定的模板调用多个页面,并且我添加了条件语句来调用每个页面及其相应的自定义帖子类型代码,但我基本上为每个条件语句重复了大部分代码,所以我想知道是否有办法简化此代码:

            <div class="row">
            <?php if (is_page(165)) {
                $args = array(
                'post_type' => 'restaurant',
                    'paged'=>$paged,
                    'orderby'=>'title',
                    'order'=>'ASC',
                'tax_query' => array(
               array(
                'taxonomy' => 'restaurant_category',
                'field' => 'slug',
                'terms' => 'sooke'
                   )
                )
            );
            $restaurants = new WP_Query( $args );
            if( $restaurants->have_posts() ) {
               while( $restaurants->have_posts() ) {
               $restaurants->the_post();
            ?>
            <div class="col-sm-4 col-md-4">
               <h1><?php the_title() ?></h1>
                <?php the_content() ?>
            </div>
               <?php
                 }
               }
               else {
                echo 'No Restaurants';
               }
               } ?>
        </div>
            <div class="row">
            <?php if (is_page(12)) {
                $args = array(
                'post_type' => 'restaurant',
                    'paged'=>$paged,
                    'orderby'=>'title',
                    'order'=>'ASC',
                'tax_query' => array(
               array(
                'taxonomy' => 'restaurant_category',
                'field' => 'slug',
                'terms' => 'chilliwack'
                   )
                )
            );
            $restaurants = new WP_Query( $args );
            if( $restaurants->have_posts() ) {
               while( $restaurants->have_posts() ) {
               $restaurants->the_post();
            ?>
            <div class="col-sm-4 col-md-4">
               <h1><?php the_title() ?></h1>
                <?php the_content() ?>
            </div>
               <?php
                 }
               }
               else {
                echo 'No Restaurants';
               }
               } ?>
        </div>

如果有一种方法可以简化此代码,以便我不会一次又一次地重复代码,我将非常感谢这样做的帮助,以便我可以学习如何以及如何不使用自定义 post 类型代码创建条件语句将来。提前感谢!

对于Dk-澳洲坚果(导致白屏的更新代码)

            <?php $terms=array('165'=>'sooke','12'=>'chilliwack');
            //now check it
            if(has_term($terms))
            { ?>
            <div class="row">
                    <?php 
                 foreach($terms as $key=>$val)
                   {
                 if(is_page($key)) {
                         $args = array(
                        'post_type' => 'restaurant',
                        'paged'=>$paged,
                        'orderby'=>'title',
                        'order'=>'ASC',
                        'tax_query' => array(
                array(
                        'taxonomy' => 'restaurant_category',
                        'field' => 'slug',
                        'terms' => $val
                            )
                        )
                    ); ?>
            </div>
我想

你正在寻找has_terms(),你可以在代码中尝试如下:因为您正在使用自定义模板,因此您可以设置这样的手动数组。

$terms=array('165'=>'sooke','12'=>'chilliwack');
//now check it
if(has_term($terms))
{ ?>
<div class="row">
            <?php 
     foreach($terms as $key=>$val)
          {
           if(is_page($key)) {
                $args = array(
                'post_type' => 'restaurant',
                    'paged'=>$paged,
                    'orderby'=>'title',
                    'order'=>'ASC',
                'tax_query' => array(
               array(
                'taxonomy' => 'restaurant_category',
                'field' => 'slug',
                'terms' => $val
                   )
                )
            );

使用 switch 语句

switch ($slug_name)
case 1 : 'chilliwack'
   my_func('chilliwack');
break;
and so on ....

// here is function
  <?php 
            function my_func($slug) {
                $args = array(
                'post_type' => 'restaurant',
                    'paged'=>$paged,
                    'orderby'=>'title',
                    'order'=>'ASC',
                'tax_query' => array(
               array(
                'taxonomy' => 'restaurant_category',
                'field' => 'slug',
                'terms' => "$slug"
                   )
                )
            );
            $restaurants = new WP_Query( $args );
            if( $restaurants->have_posts() ) {
               while( $restaurants->have_posts() ) {
               $restaurants->the_post();
            ?>
            <div class="col-sm-4 col-md-4">
               <h1><?php the_title() ?></h1>
                <?php the_content() ?>
            </div>
               <?php
                 }
               }
               else {
                echo 'No Restaurants';
               }
               }
               ?>
        </div>