4
\$\begingroup\$

I think there are some confusing and weird indentation problems in this WordPress file (single.php). Any suggestions on improving indentation and readability?

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Starkers
 * @since Starkers 3.0
 */

get_header(); ?>

<?php get_sidebar(); ?>

<div id="content">
    <?php // Set and display custom field
        $intro_image = get_post_meta($post->ID, 'Intro Image', true); ?>
        <div class="block-1">
            <img src="<?php echo $intro_image; ?>" alt="" />
        </div> <?php
    ?>

    <?php // Start The Loop
        if ( have_posts() ) while ( have_posts() ) : the_post();
    ?>  <div class="block-2 padding-top no-overlay">
            <?php the_content(); ?>
        </div><!-- .entry-content -->
    <?php endwhile; // end of the loop. ?>

    <?php //  Display previous and next posts thumbnails ?>
        <div class="block-2 border-top">
        <?php // Set and display custom field
            $mainbar_left_title = get_post_meta($post->ID, 'Mainbar Left Title', true);
            $mainbar_left_image = get_post_meta($post->ID, 'Mainbar Left Image', true); ?>
            <div class="float-left">
                <h2><?php echo $mainbar_left_title; ?></h2>
                <img src="<?php echo $mainbar_left_image ?>" alt="" />
            </div> <?php
        ?>

        <?php // Set and display custom field
            $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true);
            $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
            <div class="float-right">
                <h2><?php echo $mainbar_right_title; ?></h2>
                <img src="<?php echo $mainbar_right_image ?>" alt="" />
            </div> <?php
        ?>
    </div>

    <?php // Set and display custom field
        $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true);
        $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
        <div class="block-3 border-top">
            <h2><?php echo $mainbar_right_title; ?></h2>
            <img src="<?php echo $mainbar_right_image ?>" alt="" />
        </div> <?php
    ?>

    <?php // Start The Loop
        if ( have_posts() ) while ( have_posts() ) : the_post();
    ?>  <div class="block-4 border-top">
            <?php the_content(); ?>
        </div><!-- .entry-content -->
    <?php endwhile; // end of the loop. ?>

    <?php get_sidebar('secondary'); ?>
</div><!-- #content -->

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

You could you PHP alternate syntax

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Starkers
 * @since Starkers 3.0
 */
?>

<?php get_header(); 
<?php get_sidebar(); ?>

<div id="content">
    <?php $intro_image = get_post_meta($post->ID, 'Intro Image', true); ?>
    <div class="block-1">
        <img src="<?php echo $intro_image; ?>" alt="" />
    </div>

    <?php if ( have_posts() ? while ( have_posts() ) : the_post()): ?>  
        <div class="block-2 padding-top no-overlay">
            <?php the_content(); ?>
        </div>
    <?php endwhile; ?>

    <div class="block-2 border-top">
        <?php $mainbar_left_title = get_post_meta($post->ID, 'Mainbar Left Title', true); ?>
        <?php $mainbar_left_image = get_post_meta($post->ID, 'Mainbar Left Image', true); ?>
        <div class="float-left">
            <h2><?php echo $mainbar_left_title; ?></h2>
            <img src="<?php echo $mainbar_left_image ?>" alt="" />
        </div>

            <?php $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true); ?>
            <?php $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
        <div class="float-right">
            <h2><?php echo $mainbar_right_title; ?></h2>
            <img src="<?php echo $mainbar_right_image ?>" alt="" />
        </div>
    </div>

    <?php $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true); ?>
    <?php $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
    <div class="block-3 border-top">
        <h2><?php echo $mainbar_right_title; ?></h2>
        <img src="<?php echo $mainbar_right_image ?>" alt="" />
    </div>

    <?php if ( have_posts() ? while ( have_posts() ) : the_post()): ?>
        <div class="block-4 border-top">
            <?php the_content(); ?>
        </div>
    <?php endwhile; ?>

    <?php get_sidebar('secondary'); ?>
</div>

<?php get_footer(); ?>

This follows all WP coding standards from what I can tell and is readable. I would also suggest changing sections like this:

<?php $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true); ?>
<?php $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true); ?>
<div class="block-3 border-top">
    <h2><?php echo $mainbar_right_title; ?></h2>
    <img src="<?php echo $mainbar_right_image ?>" alt="" />
</div>

To:

<div class="block-3 border-top">
    <h2><?php echo get_post_meta($post->ID, 'Mainbar Right Title', true); ?></h2>
    <img src="<?php echo get_post_meta($post->ID, 'Mainbar Right Image', true) ?>" alt="" />
</div>

Although that's just personal preference, I find it much more readable.

\$\endgroup\$
  • \$\begingroup\$ haha well if it helps, it's considered bad practice to use braces { } for conditional or control structure in php templates. In the link you provided, although it shows the correct WP coding standard for for conditionals or control structures that have braces. The example if statement further down the page uses alternate syntax. \$\endgroup\$ – xzyfer Feb 20 '11 at 12:52
2
\$\begingroup\$
<?php // Start The Loop
   if ( have_posts() ) while ( have_posts() ) : the_post();
?>  <div class="block-2 padding-top no-overlay">
        <?php the_content(); ?>
    </div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?> 

That could become:

<?php // Start The Loop
   if (have_posts()) {
      while (have_posts()) : the_post(); ?>
         <div class="block-2 padding-top no-overlay">
            <?php the_content(); ?>
         </div><!-- .entry-content -->
      <?php endwhile; // end of the loop.
   } ?>

Still a bit confusing, but I'll keep thinking about it.

Another idea, that uses more <?php> tags:

<?php if (have_posts()) { // Start the loop ?>
   <? while (have_posts()) : the_post(); ?>
      <div class="block-2 padding-top no-overlay">
         <?php the_content(); ?>
      </div><!-- .entry-content -->
   <?php endwhile; // end of the loop. ?>
<?php } ?>

Another section change:

<?php // Set and display custom field
   $mainbar_right_title = get_post_meta($post->ID, 'Mainbar Right Title', true);
   $mainbar_right_image = get_post_meta($post->ID, 'Mainbar Right Image', true);
?>
<div class="float-right">
   <h2><?php echo $mainbar_right_title; ?></h2>
   <img src="<?php echo $mainbar_right_image ?>" alt="" />
</div>

There's a seemingly useless <?php \n ?> sitting there. It looks like the idea was to close a code block, but it doesn't do anything. I removed that and changed the indentation so it didn't look like a block.

\$\endgroup\$
  • \$\begingroup\$ I think looks cleaner but is there a way of making the end match horizontally with the top? \$\endgroup\$ – janoChen Feb 18 '11 at 14:09
  • \$\begingroup\$ I posted another idea - it would be easier to read as well if you can use <? ?> instead of <?php ?>. Is that an option? \$\endgroup\$ – Michael K Feb 18 '11 at 14:17
  • \$\begingroup\$ Sorry it is against the law: codex.wordpress.org/WordPress_Coding_Standards (I will wait more answers for 2 days if there's no more I will check yours. Thanks.) \$\endgroup\$ – janoChen Feb 18 '11 at 14:29
  • \$\begingroup\$ @janoChen: Interesting read. \$\endgroup\$ – Michael K Feb 18 '11 at 14:33

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.