-1

I'm trying to add pagination to my [shortcut]. This is my code:

<?php }

//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
    <?php 
    extract( shortcode_atts(array('show' =>''), $atts ));   
    extract( shortcode_atts(array('brand' =>''), $atts ));
    $loop = new WP_Query( array(    
    'post_type' => 'auto',  
    'auto-brand' => $brand, 
    'posts_per_page'=>$show,    
    'offset' => 0
));// $count = $loop->found_posts; echo $count;
    while ($loop->have_posts()) : $loop->the_post();
    global $post; ?>
        <div class="col-list-3">
          <div class="featured-car-list">
                <div class="featured-car-img">
                    <a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
                        <?php if(has_post_thumbnail()):
                                the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
                                else:
                                echo "<div class='is-empty-img-box'></div>";
                                endif;
                         ?>
                    </a>
                    <?php carforyou_AutoType(); ?>
                    <div class="compare_item">
                            <div class="checkbox">
                                <button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
                            </div>
                        </div>
                 </div>
                <div class="featured-car-content">
                    <h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
                    <div class="price_info">
                        <?php  if(!empty($post->DREAM_auto_price)): ?>
                        <p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
                        <?php endif; ?>
                        <div class="car-location">
                        <?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
                                foreach($term_list as $term_single) 
                                    $location = $term_single->name;
                        ?>
                        <?php  if(!empty($location)): ?>
                        <span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
                        <?php endif; ?>
                        </div>
                    </div>
                    <ul>
                        <?php carforyou_featuredList(); ?> 
                    </ul>
                </div>
          </div>
        </div>
    <?php endwhile; wp_reset_query(); ?>   
</div>
<?php }

Above code using [latestcars show="10" brand="ford"] displays 10 cars made by ford. I want to add pagination, so I have added folllwing modification:

<?php }

//LatestCar Function
function carforyou_LatestCar($atts){
ob_start();?>
<div class="row">
    <?php 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    extract( shortcode_atts(array('show' =>''), $atts ));   
    extract( shortcode_atts(array('brand' =>''), $atts ));
    $loop = new WP_Query( array(    
    'post_type' => 'auto',  
    'auto-brand' => $brand,
    'paged' =>$paged,   
    'posts_per_page'=>$show,    
    'offset' => 0
));
// $count = $loop->found_posts; echo $count;



    while ($loop->have_posts()) : $loop->the_post();
    global $paged, $post;

    ?>
        <div class="col-list-3">
          <div class="featured-car-list">
                <div class="featured-car-img">
                    <a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
                        <?php if(has_post_thumbnail()):
                                the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
                                else:
                                echo "<div class='is-empty-img-box'></div>";
                                endif;
                         ?>
                    </a>
                    <?php carforyou_AutoType(); ?>
                    <div class="compare_item">
                            <div class="checkbox">
                                <button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
                            </div>
                        </div>
                 </div>
                <div class="featured-car-content">
                    <h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
                    <div class="price_info">
                        <?php  if(!empty($post->DREAM_auto_price)): ?>
                        <p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
                        <?php endif; ?>
                        <div class="car-location">
                        <?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
                                foreach($term_list as $term_single) 
                                    $location = $term_single->name;
                        ?>
                        <?php  if(!empty($location)): ?>
                        <span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
                        <?php endif; ?>
                        </div>
                    </div>
                    <ul>
                        <?php carforyou_featuredList(); ?> 
                    </ul>
                </div>
          </div>
        </div>
    <?php endwhile; 
        carforyou_pagination();
        else:
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        extract( shortcode_atts(array('show' =>''), $atts ));   
        extract( shortcode_atts(array('brand' =>''), $atts ));
        $loop = new WP_Query( array(    
        'post_type' => 'auto',  
        'auto-brand' => $brand,
        'paged' =>$paged,   
        'posts_per_page'=>$show,    
        'offset' => 0
        ));
        while ($loop->have_posts()) : $loop->the_post();
        ?>
        <div class="col-list-3">
          <div class="featured-car-list">
                <div class="featured-car-img">
                    <a alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" href="<?php the_permalink();?>">
                        <?php if(has_post_thumbnail()):
                                the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
                                else:
                                echo "<div class='is-empty-img-box'></div>";
                                endif;
                         ?>
                    </a>
                    <?php carforyou_AutoType(); ?>
                    <div class="compare_item">
                            <div class="checkbox">
                                <button id="compare_auto_btn" onclick="<?php echo esc_js('javascript:productCompare('.$post->ID.')'); ?>"><?php esc_html_e('Compare','carforyou'); ?></button>
                            </div>
                        </div>
                 </div>
                <div class="featured-car-content">
                    <h6><a title="<?php echo get_the_title(); ?>" href="<?php the_permalink(); ?>"><?php $title = get_the_title(); echo mb_strimwidth($title, 0, 30, '...'); ?></a></h6>
                    <div class="price_info">
                        <?php  if(!empty($post->DREAM_auto_price)): ?>
                        <p class="featured-price"><?php carforyou_curcy_prefix(); ?><?php echo number_format_i18n(esc_html($post->DREAM_auto_price)); ?></p>
                        <?php endif; ?>
                        <div class="car-location">
                        <?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
                                foreach($term_list as $term_single) 
                                    $location = $term_single->name;
                        ?>
                        <?php  if(!empty($location)): ?>
                        <span><i class="fa fa-map-marker" aria-hidden="true"></i> <?php echo esc_html($location); ?> </span>
                        <?php endif; ?>
                        </div>
                    </div>
                    <ul>
                        <?php carforyou_featuredList(); ?> 
                    </ul>
                </div>
          </div>
        </div>
        <?php endwhile; 
         carforyou_pagination(); 
    endif;
}
?>

Above solution returns following error code which I can't trace:

PHP Parse error: syntax error, unexpected 'else' (T_ELSE) in /var/www/clients/clientxxx/webxxx/web/wp-content/themes/xxxxxx/functions/basic-functions.php on line 439

Line 439 is "else:"

    <?php endwhile; 
        carforyou_pagination();
        else:
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
(...)

Any advice? Thanks

Kris
  • 85
  • 7
  • It's not an issue in this file.. it seems to be an error in `basic-functions.php > carforyou_pagination()` – Satish Saini Mar 07 '19 at 23:27
  • The error message is quite clear - there is an unexpected `else` in basic-functions.php. So check the if-else statements in *that* code for the problem. – FluffyKitten Mar 07 '19 at 23:28

1 Answers1

1

Strip out the HTML tags and it becomes pretty clear that your if...else control structure is not correct. The else in question has no corresponding if structure before it.

while ($loop->have_posts()) : $loop->the_post();
global $paged, $post;?>
<?php if(has_post_thumbnail()):             //OPEN IF ONE
the_post_thumbnail('carforyou_small', array('class' => 'img-responsive'));
else:
echo "<div class='is-empty-img-box'></div>";
endif;                                       //CLOSE IF ONE
?>
<?php carforyou_AutoType(); ?>

<?php  if(!empty($post->DREAM_auto_price)): ?> //OPEN IF TWO
<?php endif; ?>                                //CLOSE IF TWO
<?php $term_list = wp_get_post_terms($post->ID, 'auto-location', array("fields" => "all"));
foreach($term_list as $term_single) 
$location = $term_single->name;
?>
<?php  if(!empty($location)): ?>              //OPEN IF THREE
<?php endif; ?>                               //CLOSE IF THREE
<?php carforyou_featuredList(); ?> 
<?php endwhile; 
carforyou_pagination();
else:                                      //SO WHERE IS IF FOUR???
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
extract( shortcode_atts(array('show' =>''), $atts ));  
...
parttimeturtle
  • 1,125
  • 7
  • 22
  • Nice one, cheers mate! – Kris Mar 08 '19 at 08:29
  • Questions that are about basic PHP syntax errors should not be answered. They should be closed as a duplicate of [PHP Parse/Syntax Errors; and How to solve them?](//stackoverflow.com/questions/18050071) only. If you want to point out their mistake, just post a comment. See: [Should one advise on off topic questions?](//meta.stackoverflow.com/q/276572/1768232). Off-topic questions can be closed and deleted, which could nullify your contribution anyway. – John Conde Mar 08 '19 at 11:42
  • Would have loved to post a comment but up until about 10 minutes ago I didn't have the reputation – parttimeturtle Mar 08 '19 at 17:06