S Biswas S Biswas - 28 days ago 10
PHP Question

How to add class to first wordpress custom post

I am trying to add an additional class to the very first post of a custom post in wordpress and I am using the following code on my template part that I got from sof site but it is showing an error.


Parse error: syntax error, unexpected '<' in
C:\xampp\htdocs\e24xp\wp-content\themes\e24x\testimonial.php on line
19


<?php
$args = array( 'post_type' => 'cc_testimonial', 'posts_per_page' => 1, "order" => "DESC");
$query = new WP_Query( $args );
$cc = count($query);
if ( $query->have_posts() ) {
$i=0;
while ( $query->have_posts() ) {
$query->the_post();
<div class="testimonialitem item <?php echo ($i==0)?'active':''; ?>">//SHOWING ERROR
<div class="testimonial-content">
<p><?php the_title( );?></p>
</div>
</div><!--/.testimonialitem-->
$i++;
}
}
wp_reset_query();
wp_reset_postdata();
?>


What is wrong in this code??

Answer

You forgot to close PHP before writing HTML

<?php         
$args = array( 'post_type' => 'cc_testimonial', 'posts_per_page' => 1, "order" => "DESC");
$query = new WP_Query( $args );
$cc = count($query);
if ( $query->have_posts() ) {
$i=0;
while ( $query->have_posts() ) { 
$query->the_post(); 

// need to close PHP here ?>

<div class="testimonialitem item <?php echo ($i==0)?'active':''; ?>"> <!-- this is Html comment SHOWING ERROR -->
                <div class="testimonial-content">
                    <p><?php the_title( );?></p>
                </div>                          
            </div><!--/.testimonialitem-->       

<?php // and open it here again
  $i++; 
   }
}
wp_reset_query();
wp_reset_postdata();
?>