theme options echoing multiple times

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

Read More

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.";}

Related posts

Leave a Reply

1 comment

  1. 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;