PHP conditional statements within Wordpress's function.php file not working

ぐ巨炮叔叔 提交于 2019-12-11 16:35:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!