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.

I tried to strip down the Twenty Eleven theme options page and add my own fields correction: I followed a tutorial somewhere, but when I try to echo the data, it comes out multiple times.

Here is my theme-options.php: http://pastebin.com/HSZM56jA

This is how I am echoing it:

<?php
    $options = get_option('gavsiu_theme_options');
    echo $options['message-primary'];
    echo $options['message-secondary'];
?>

It comes out 11 times.

This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message. This is the main message.This is the secondary message.

I checked mySQL and the data is saved once. There is no duplication or error in saving the sentences.

print_r $options reveals that it prints out the array numerous times, so when I echo 'message-primary', it echoes each match from each array.


I'm echoing this in the front page. Saving the fields again did not change anything.

As I said, it is saved correctly into the database. In wp_options > gavsiu_theme_options :

a:2:{s:15:"message-primary";s:25:"This is the main message.";s:17:"message-secondary";s:30:"This is the secondary message.";}
share|improve this question
    
(+1) for writing a good to read and detailed Q. Short Q: Where do you echo the data? Further: Does this value increase with each save? In this case I'd say you're saving it wrong. –  kaiser Sep 9 '11 at 17:06
    
are you perhaps echoing the options inside the WordPress loop? I've pasted your code into an empty theme and I don't see this same behavior. –  Milo Sep 9 '11 at 19:43
    
That may be the problem. I have to go to work now so I can't check. If I were to echo this in a loop, how can I only echo 1 instance? –  gavsiu Sep 9 '11 at 19:47
    
check if( $wp_query->current_post == 0 ) to only output it in the first iteration of the loop. –  Milo Sep 9 '11 at 23:35
    
Milo, your solution worked. Thanks. You should put it as an answer so I can accept. –  gavsiu Sep 10 '11 at 5:25
add comment

1 Answer

up vote 0 down vote accepted

The options are being output within the loop, so are being repeated for each iteration of the loop.

To check within the loop and only output something on the first iteration:

while ( have_posts() ) : the_post();

    if( $wp_query->current_post == 0 ):
        // this is the first post
        // output your options
    endif;

    // other loop stuff, title, content, etc.

endwhile;

similarly, to check if you are on the last post of the loop:

while ( have_posts() ) : the_post();

    if( $wp_query->current_post == ( $wp_query->post_count - 1 ) ):
        // this is the last post
    endif;

endwhile;
share|improve this answer
    
I have since moved the content out of the loop so I don't have to add the extra code. –  gavsiu Sep 12 '11 at 4:40
add comment

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.