-1

I have a problem I'm trying to solve, but with no avail because php is really not my field. I already searched the forums and found a few solutions to similar problems, but I couldn't get the to work.

I created a Theme Options Page where you can for ex. upload a Logo to be used on the Website. I created a check box and want to delete the logo if the check box is checked. Anyway, the logo is deleted when I click Save changes, but regardless if I checked the check box or not.

This is my code:

// LOGO
function logo_setting() {
    $options = get_option('theme_options');
    if (empty($options['logo'])) :
        $options['logo'] = '';
    else :
        echo "<img src='{$options['logo']}' width='205px' /><br>";

        //DELETE OPTION
        if (!isset( $options['del_logo'] ) ) :
                $options['del_logo'] = '';
                echo "<br>Remove logo?&nbsp;";
                ?> <input type="checkbox" id="del_logo" name="theme_options[del_logo]" value="1" <?php checked( true, $options['del_logo'] ); ?> /><?php

                echo "<br>If logo is removed, Site Title will be used.";
            else :
                $options = get_option('theme_options');
                $options['logo'] = '';
                update_option('theme_options', $options);
                echo "Logo removed";
        endif;
        $options['del_logo'] = ( $options['del_logo'] == 1 ? 1 : 0 );
        return $options;

    endif;
    echo "<input type='file' name='logo' />";
}

I hope someone can explain to me what I'm doing wrong and how I should do it properly.

4

1 回答 1

1

OP写道:

解决方案:

function logo_setting() {
    $options = get_option('theme_options');
    if (empty($options['logo'])) :
    $options['logo'] = '';
    echo "Site title is used.<br>";
    else :
    echo "<img src='{$options['logo']}' width='205px' /><br>";
    endif;  
    echo "<input type='file' name='logo' />";

    if (empty($options['hide_logo'])) :
    $options['hide_logo'] = '';
    endif;
    echo '<br><br>Show site title instead of logo?&nbsp;<input name="theme_options[hide_logo]" id="hide_logo" type="checkbox" value="1" ' . checked( 1, $options['hide_logo'], false ) . ' />';
}

在 html 中:

 <?php if (empty($options['logo']) || isset($options['hide_logo'])) :
    bloginfo('name');
else : ?>
    <img src="<?php echo $options['logo']; ?>" alt="Logo" />
<?php endif; ?>
于 2015-07-03T18:40:15.117 回答