将if语句添加到PHP模板

时间:2017-07-20 09:42:00

标签: php wordpress if-statement visual-composer

我有以下模板,我想添加if语句,以确定基于$ num变量显示的布局。但是,我无法使用if,elseif语句来工作! (新手!)非常感谢...

这是当前的模板:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.urls = functions.storage.object().onChange( event => {
    const file = event.data;
    const metadata = file.metadata;
    console.log(metadata);
return admin.database().ref("/").set(metadata); });

文章布局是我想根据$ num变量改变的。 例如,如果$ num == 2,则文章布局应为:

<?php  
   $count = 1;
   $query_args = array('post_type' => 'bunch_team' , 'showposts' => $num, 
   'order_by' => $sort , 'order' => $order);
   if( $cat ) $query_args['team_category'] = $cat;
   $query = new WP_Query($query_args) ; 
   ob_start() ;?>
   <?php if($query->have_posts()):  ?>   <!--Our Team Section-->
   <section class="team-section">
    <div class="auto-container">
        <div class="sec-title">
            <h2><?php echo balanceTags($title);?></h2>
            <div class="separator"></div>
            <div class="heading-text"><?php echo balanceTags($sub_title);?>
            </div>
        </div>

        <div class="row clearfix">

            <?php while($query->have_posts()): $query->the_post();
                global $post ; 
                $teams_meta = _WSH()->get_meta();
                $post_thumbnail_id = get_post_thumbnail_id($post->ID);
                $post_thumbnail_url = wp_get_attachment_url( 
                $post_thumbnail_id );
               ?>  
            <!--Member-->
            <article class="col-md-3 col-sm-6 col-xs-12 member-column">
                <div class="inner-box">
                    <figure class="image">
                        <?php the_post_thumbnail('convo_size_team');?>
                    </figure>
                    <div class="member-title">
                        <h4><?php the_title();?></h4>
                        <p><?php echo convo_set($teams_meta, 
                        'designation');?></p>
                    </div>
                    <div class="member-desc">
                        <p><?php echo convo_trim(get_the_excerpt(), 
                        $text_limit);?></p>
                    </div>
                    <?php if($socials = convo_set($teams_meta, 
                    'bunch_team_social')):?>
                    <ul class="social-links clearfix">

                        <?php foreach($socials as $key => $value):?>
                                    <li><a href="<?php echo 
                                    esc_url(convo_set($value, 
                                    'social_link'));?>" class="fa <?php echo 
                                     convo_set($value, 'social_icon');?>">
                                     </a></li>
                            <?php endforeach;?>
                    </ul>
                    <?php endif;?>
                </div>
            </article>
        <?php endwhile;?>   
        </div>
    </div>
</section>


<?php endif; ?>
<?php 
   wp_reset_postdata();
   $output = ob_get_contents(); 
   ob_end_clean(); 
   return $output ; ?>

如果$ num == 3,则文章布局应为:

                <article class="col-md-6 col-sm-6 col-xs-12 member-column">
                <div class="inner-box">
                    <figure class="image">
                        <?php the_post_thumbnail('convo_size_team');?>
                    </figure>
                    <div class="member-title">
                        <h4><?php the_title();?></h4>
                        <p><?php echo convo_set($teams_meta, 'designation');?></p>
                    </div>
                    <div class="member-desc">
                        <p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
                    </div>
                    <?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
                    <ul class="social-links clearfix">

                        <?php foreach($socials as $key => $value):?>
                                    <li><a href="<?php echo esc_url(convo_set($value, 'social_link'));?>" class="fa <?php echo convo_set($value, 'social_icon');?>"></a></li>
                            <?php endforeach;?>
                    </ul>
                    <?php endif;?>
                </div>
            </article>

如果$ num == 4,则文章布局应为:

                <article class="col-md-4 col-sm-6 col-xs-12 member-column">
                <div class="inner-box">
                    <figure class="image">
                        <?php the_post_thumbnail('convo_size_team');?>
                    </figure>
                    <div class="member-title">
                        <h4><?php the_title();?></h4>
                        <p><?php echo convo_set($teams_meta, 'designation');?></p>
                    </div>
                    <div class="member-desc">
                        <p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
                    </div>
                    <?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
                    <ul class="social-links clearfix">

                        <?php foreach($socials as $key => $value):?>
                                    <li><a href="<?php echo esc_url(convo_set($value, 'social_link'));?>" class="fa <?php echo convo_set($value, 'social_icon');?>"></a></li>
                            <?php endforeach;?>
                    </ul>
                    <?php endif;?>
                </div>
            </article>

非常感谢任何帮助。我大致知道如何做,但是不能让if语句正确显示基于$ num变量的正确布局。

干杯!

1 个答案:

答案 0 :(得分:0)

这样的东西?

<?php
$count = 1;

$query_args = array(
  'post_type' => 'bunch_team',
  'showposts' => $num,
  'order_by' => $sort,
  'order' => $order
);

if ( $cat ) {
  $query_args['team_category'] = $cat;
}

$query = new WP_Query( $query_args ); ?>
<?php if($query->have_posts()):  ?>   <!--Our Team Section-->
  <section class="team-section">
    <div class="auto-container">
      <div class="sec-title">
        <h2><?php echo balanceTags($title);?></h2>
        <div class="separator"></div>
        <div class="heading-text"><?php echo balanceTags($sub_title);?>
        </div>
      </div>

      <div class="row clearfix">
        <?php while($query->have_posts()) : $query->the_post();
          global $post;
          $teams_meta = _WSH()->get_meta();
          $post_thumbnail_id = get_post_thumbnail_id($post->ID);
          $post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
          if ( $num === 2 ) { // Must be number! Instead use '2'
          ?>
          <article class="col-md-6 col-sm-6 col-xs-12 member-column">
              <div class="inner-box">
                  <figure class="image">
                      <?php the_post_thumbnail('convo_size_team');?>
                  </figure>
                  <div class="member-title">
                      <h4><?php the_title();?></h4>
                      <p><?php echo convo_set($teams_meta, 'designation');?></p>
                  </div>
                  <div class="member-desc">
                      <p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
                  </div>
                  <?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
                  <ul class="social-links clearfix">

                      <?php foreach($socials as $key => $value):?>
                                  <li><a href="<?php echo esc_url(convo_set($value, 'social_link'));?>" class="fa <?php echo convo_set($value, 'social_icon');?>"></a></li>
                          <?php endforeach;?>
                  </ul>
                  <?php endif;?>
              </div>
          </article>
          <?php 
          } elseif ( $num === 3 ) {
          ?>
          <article class="col-md-4 col-sm-6 col-xs-12 member-column">
              <div class="inner-box">
                  <figure class="image">
                      <?php the_post_thumbnail('convo_size_team');?>
                  </figure>
                  <div class="member-title">
                      <h4><?php the_title();?></h4>
                      <p><?php echo convo_set($teams_meta, 'designation');?></p>
                  </div>
                  <div class="member-desc">
                      <p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
                  </div>
                  <?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
                  <ul class="social-links clearfix">

                      <?php foreach($socials as $key => $value):?>
                                  <li><a href="<?php echo esc_url(convo_set($value, 'social_link'));?>" class="fa <?php echo convo_set($value, 'social_icon');?>"></a></li>
                          <?php endforeach;?>
                  </ul>
                  <?php endif;?>
              </div>
          </article>
          <?php 
          } elseif ( $num === 4 ) {
          ?>
            <article class="col-md-3 col-sm-6 col-xs-12 member-column">
              <div class="inner-box">
                  <figure class="image">
                      <?php the_post_thumbnail('convo_size_team');?>
                  </figure>
                  <div class="member-title">
                      <h4><?php the_title();?></h4>
                      <p><?php echo convo_set($teams_meta, 'designation');?></p>
                  </div>
                  <div class="member-desc">
                      <p><?php echo convo_trim(get_the_excerpt(), $text_limit);?></p>
                  </div>
                  <?php if($socials = convo_set($teams_meta, 'bunch_team_social')):?>
                  <ul class="social-links clearfix">

                      <?php foreach($socials as $key => $value):?>
                                  <li><a href="<?php echo esc_url(convo_set($value, 'social_link'));?>" class="fa <?php echo convo_set($value, 'social_icon');?>"></a></li>
                          <?php endforeach;?>
                  </ul>
                  <?php endif;?>
              </div>
            </article>
          <?php 
          } else {
            <article class="col-md-3 col-sm-6 col-xs-12 member-column">
            <div class="inner-box">
              <figure class="image">
                <?php the_post_thumbnail('convo_size_team');?>
              </figure>
              <div class="member-title">
                <h4><?php the_title();?></h4>
                <p><?php echo convo_set($teams_meta, 
                  'designation');?></p>
                </div>
                <div class="member-desc">
                  <p><?php echo convo_trim(get_the_excerpt(), 
                    $text_limit);?></p>
                  </div>
                  <?php if($socials = convo_set($teams_meta, 
                  'bunch_team_social')):?>
                  <ul class="social-links clearfix">

                    <?php foreach($socials as $key => $value):?>
                      <li><a href="<?php echo 
                        esc_url(convo_set($value, 
                        'social_link'));?>" class="fa <?php echo 
                        convo_set($value, 'social_icon');?>">
                      </a></li>
                    <?php endforeach;?>
                  </ul>
                <?php endif;?>
              </div>
            </article>
          <?php 
          }
          endwhile;?> 
        </div>
      </div>
    </section>
<?php endif;
wp_reset_postdata();

你需要以某种方式提供$num,如果是数字或字符串,请小心。

页面模板中也不需要输出缓冲。