6
\$\begingroup\$

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:

<?php
/**
 * Template Name: Home
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>

<div id="sidebar">
    <?php get_sidebar(); ?>
</div><!-- #sidebar -->

<div id="content">

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=Profile');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-1">
        <?php the_post_thumbnail('large'); ?>
    </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=Tagline');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-2 padding-top">
        <h2><?php the_title(); ?></h2>
        <p><?php the_content(); ?></p>
    </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=Themep');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-2 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    <?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; ?>

        <?php // Create and run custom loop
            $custom_posts = new WP_Query();
            $custom_posts->query('post_type=page_content&page_sections=Theme Child Right');
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <div class="float-right">
                <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>
        </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=FromBlog');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>  <div class="block-3 border-top">
            <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <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=Featured');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-2 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <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('large'); ?></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=Last');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-3 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <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>
        <p><?php the_meta(); ?></p>
    </div>
    <?php endwhile; ?>

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=Lastest');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    <div class="block-7 border-top">
        <h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <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; ?>

</div><!-- #content -->

<?php get_footer(); ?>
\$\endgroup\$
2
\$\begingroup\$

Some general tips

  1. Mind your nesting. Indent at new block-level tags always, and at line-level when it aids legibility
  2. Wrap where it makes sense: between arguments, at attributes, etc
  3. Be consistent!

For example, this chunk

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

I would rewrite like so

<?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; ?>
\$\endgroup\$
0
\$\begingroup\$

DRY is a term coined by developers who believe that repetetive code is bad. I'm one of them :)

First the code goes like this:

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Profile');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

Then

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Tagline');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

Then

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Themep');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

Then

    <?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();
    ?>
    [..snip html (and some php)...]
    <?php endwhile; ?>

And again...

    <?php // Create and run custom loop
        $custom_posts = new WP_Query();
        $custom_posts->query('post_type=page_content&page_sections=Theme Child Right');
        while ($custom_posts->have_posts()) : $custom_posts->the_post();
    ?>
    [..snip html (and some php)...]
    <?php endwhile; ?>

And yet again...

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=FromBlog');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

One more time...

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Featured');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

They just keep coming...

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Last');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

Finally, the last one...

<?php // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query('post_type=page_content&page_sections=Lastest');
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
?>
[..snip html (and some php)...]
<?php endwhile; ?>

I suppose it is clear now that the code is very repetitive. If you aren't convinced that this pattern is bad, consider reading up on cut'n'paste programming.

If you are using PHP 5.3 or greater (which is most likely today), you can rewrite this stuff using anonymous functions as callbacks:

function print_posts($query, $content_cb) {
    // Create and run custom loop
    $custom_posts = new WP_Query();
    $custom_posts->query($query);
    while ($custom_posts->have_posts()) {
        $custom_posts->the_post();
        $content_cb();
    }
}

...and then just use the function:

print_posts(
    'post_type=page_content&page_sections=Profile',
    function() {
        echo '<div class="block-1">';
        echo the_post_thumbnail('large');
        echo '</div>'
    }
);

...and so on.

This will eliminate a lot of boilerplate code that is just getting in the way and make the whole thing somewhat shorter, which by itself is something to be desired, since the number of bugs is proportional to the number of lines (or statements, at least).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.