patternphpMinor
modify custom loops to improve readability and efficiency for Wordpress?
Viewed 0 times
readabilityloopswordpressimprovecustomforandefficiencymodify
Problem
The following code retrieve custom post types with their custom taxonomy.
I'm just a beginner in PHP and I would like to know tips in order to improve readability and perhaps efficiency.
home.php:
```
query('post_type=page_content&page_sections=Profile');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
query('post_type=page_content&page_sections=Tagline');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
query('post_type=page_content&page_sections=Themep');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Theme Child Right');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=FromBlog');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Featured');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Last');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Lastest');
I'm just a beginner in PHP and I would like to know tips in order to improve readability and perhaps efficiency.
home.php:
```
query('post_type=page_content&page_sections=Profile');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
query('post_type=page_content&page_sections=Tagline');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
query('post_type=page_content&page_sections=Themep');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Theme Child Right');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
query('post_type=page_content&page_sections=FromBlog');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Featured');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Last');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
" title="" rel="bookmark">
query('post_type=page_content&page_sections=Lastest');
Solution
Some general tips
For example, this chunk
I would rewrite like so
- Mind your nesting. Indent at new block-level tags always, and at line-level when it aids legibility
- Wrap where it makes sense: between arguments, at attributes, etc
- Be consistent!
For example, this chunk
query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
" title="" rel="bookmark">
I would rewrite like so
query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
"
title=""
rel="bookmark">
Code Snippets
<?php // Create and run custom loop
$custom_posts = new WP_Query();
$custom_posts->query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?> <div class="float-left">
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_post_thumbnail(); ?></a>
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; ?><?php // Create and run custom loop
$custom_posts = new WP_Query();
$custom_posts->query('post_type=page_content&page_sections=ThemeCL');
while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
<div class="float-left">
<a href="<?php the_permalink(); ?>"
title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>"
rel="bookmark"><?php the_post_thumbnail(); ?></a>
<p>
<?php the_excerpt(); ?>
</p>
</div>
<?php endwhile; ?>Context
StackExchange Code Review Q#657, answer score: 2
Revisions (0)
No revisions yet.