问题
I want to do the following PHP statement within function.php
: If there's an uploaded image for #header h1 a
's background, indent the text (remove)...If not (else), keep the text (don't indent it).
I'm not very familiar with PHP but this is what I did:
<?php
// Include functions of Theme Options
require_once(TEMPLATEPATH . '/functions/admin-menu.php');
// If there's an image for the site title, remove it's text
function logo_exists() {
?><style type="text/css">
#header h1 a {
<?php
if ( $options['logo'] == NULL ) { ?>
float: left;
text-indent: 0;
<?php } else { ?>
background: url(<?php echo $options['logo']; ?>) no-repeat scroll 0 0;
float: left;
text-indent: -9999px;
width: 160px;
height: 80px;
}
<?php } ?>
</style><?php
}
header.php:
// Initiate Theme Options
$options = get_option('plugin_options');
var_dump($options['logo'])
?>
<style>
body {
background-color: <?php echo $options['color_scheme']; ?>
}
</style>
</head>
<body <?php body_class(); ?>>
<div id="wrapper">
<div id="header">
<h1>
<a href="<?php echo home_url( '/' ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
</h1>
(I'm not sure if it is useful information but plugin_options is initialised just before the <style>
tags.)
I did var_dump
and #options['logo']
displays:
string(63) "http://localhost/wordpress/wp-content/uploads/2010/12/logo3.png"
(So the image is working)
But right now I just see the Wordpress site title
Do I have to "activate" or "initiate" the function **logo_exists? or is there another problem?**
(Everything works ok if I don't use the PHP If statements and declaring $options[logo] directly from header.php)
Thanks in advance!
回答1:
$options
doesn't exist in the scope of that function. You need to add
global $options;
just after the function declaration, so that it picks up the $options
variable from the include. Like this:
function logo_exists() {
global $options;
?><style type="text/css">
...
来源:https://stackoverflow.com/questions/4482295/php-conditional-statements-within-wordpresss-function-php-file-not-working