jeudi 20 juillet 2017

Adding if statements to PHP template

I have the below template that I would like to add if statements to in order to determine the layout that is displayed based on the $num variable. However, I cannot get my if, elseif statements to work! (newbie!) Help much appreciated...

This is the current template:

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

The article layout is what I would like to change depending on the $num variable. For example, if $num == 2, the article layout should be:

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

If $num == 3, the article layout should be:

                <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 == 4, the article layout should be:

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

Any help would be greatly appreciated. I know roughly how to do i, but cannot got the if statement to correctly show ONLY the right layout based on the $num variable.

Cheers!

Aucun commentaire:

Enregistrer un commentaire