0

所以我仍然掌握 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>

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

对于 Dk-Macadamia(导致白屏的更新代码)

            <?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>
4

2 回答 2

0

使用 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>
于 2013-11-04T05:14:55.623 回答
0

我认为您正在寻找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
                   )
                )
            );
于 2013-11-04T03:41:04.800 回答