How would I write this PHP code for calling category posts in WordPress better? -
i new php , tried hand @ coding call recent posts in category, seems got echo loop.
how optimize following code did not look, well, does?
<?php $cat_id = 3; $latest_cat_post = new wp_query( array('posts_per_page' => 1, 'category__in' => array($cat_id))); if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post(); echo '<a href="'; the_permalink(); echo '">'; if ( has_post_thumbnail() ) { the_post_thumbnail(); } echo '</a>'; echo '<div class="widget-box-text">' echo '<a href="'; the_permalink(); echo '">'; the_title(); echo '</a>'; the_excerpt(); echo '</div><!-- widget-box-text -->' endwhile; endif; ?> thanks much, forward learning programming , want make code @ least conform kind of norm.
you have format , indent code , use php templating instead of echo:
<?php $cat_id = 3; $query = new wp_query(array( 'posts_per_page' => 1, 'category__in' => $cat_id )); ?> <?php while ($query->have_posts()): $query->the_post(); ?> <a href="<?php the_permalink(); ?>"></a> <?php if (has_post_thumbnail()) the_post_thumbnail(); ?> <div class="widget-box-text"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_excerpt(); ?> </div> <?php endwhile; ?>
Comments
Post a Comment