Wordpress function.php 文件中的 PHP 条件语句不起作用

发布于 2024-10-08 18:43:09 字数 1996 浏览 0 评论 0原文

我想在 function.php 中执行以下 PHP 语句:如果有上传的 #header h1 a 背景图像,请缩进文本(删除)...如果不是(否则),请保留文本(不要缩进)。

我对 PHP 不太熟悉,但这就是我所做的:

<?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>

(我不确定它是否是有用的信息,但 plugin_options 是在

我做了 var_dump#options['logo'] 显示:(

string(63) "http://localhost/wordpress/wp-content/uploads/2010/12/logo3.png" 

所以图像正在工作)

但现在我只看到 WordPress site title

我是否必须“激活”或“启动”功能 **logo_exists? 还是还有其他问题?**

(如果我不使用 PHP If 语句并直接从 header.php 声明 $options[logo] ,一切正常)

提前致谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2024-10-15 18:43:09

$options 不存在于该函数的范围内。您需要

global $options;

在函数声明之后添加,以便它从包含中获取 $options 变量。像这样:

function logo_exists() {
    global $options;

    ?><style type="text/css">
    ...

$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">
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文