Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The Puzzle: I am currently working on a music blog that has various venues and numerous locations that are laid out at the top of each post as follows:

USA.California.Los Angeles. Hollywood Bowl

Note: Everything except the venue name (Hollywood Bowl) is bold.

In order to limit the number of calls to the server I have clumped all the title information into a single array (as seen below). enter image description here enter image description here

As of now I am simply calling everything in the array consecutively:

<?php $custom_fields = get_post_custom($post->ID); 
    for ($i = 1; $i <= 4; $i++)  {
        if(isset($custom_fields["rw_location_$i"])){ ?>
            <h1><?php echo get_post_meta ($post->ID,"rw_location_$i", true ); ?></h1>
            <?php } ?>
            <?php } ?>

The Question: How do I still make a "single call" but create a unique class (h1,h2,h3) for different $i? For example have the $i = 1 be a <h1></h1> tag and $i = 2,3,4 be <h2></h2> tags.

I hope my question is clear enough, if not I'd be happy to elaborate further.

(P.S. I know the numbering is off, but I'm sure we can all overlook that)

share|improve this question
    
The quick fix.. <h<?php echo $i; ?>><?php echo get_post_meta ($post->ID,"rw_location_$i", true ); ?></h<?php echo $i; ?>> .. –  t31os Aug 7 '11 at 16:26

1 Answer 1

up vote 0 down vote accepted

To are still call database inside the by get_post_meta() where you already have the values in $custom_fields variable. Try something like this:

 <?php

    $custom_fields = get_post_custom($post->ID); 
      for ($i = 1; $i <= 4; $i++)  {
       if(isset($custom_fields["rw_location_$i"][0])){ 
        if($i==1){ echo '<h1>';} else {echo '<h2>';}
        echo $custom_fields["rw_location_$i"][0] 
        if($i==1) {echo '</h1>'; }else {echo '</h2>'};
       }
    } 
  ?>

If I get your question right. Are you looking to echo <h1> for the first iteration and <h2> for all other iteration?

share|improve this answer
    
Exactly what I was looking for! Just one minor related question. Is it proper to echo so many times? I mean I see no reason why not but wanted to double check if this was the only practical method for fulfilling my needs. I don't know why but I always get the feeling adding too many echoes it's like adding too many unnecessary div's to a html doc. –  mmaximalist Aug 7 '11 at 17:30
    
I see no problem with that. I don't know if its affect the performance or not. You can do concatenation php.net/manual/en/language.operators.string.php to save the html in a variable and echo outside of the loop if you want. Thanks! :) –  Sisir Aug 8 '11 at 9:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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