Wordpress的function.php文件中的PHP条件语句不起作用 [英] PHP conditional statements within Wordpress's function.php file not working

查看:98
本文介绍了Wordpress的function.php文件中的PHP条件语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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>

(我不确定是否有用的信息,但是plugin_options在<$ c之前初始化$ c>< style> 标签。)

(I'm not sure if it is useful information but plugin_options is initialised just before the <style> tags.)

我做了 var_dump #options ['徽标'] 显示:

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

(因此图像正常工作)

但是现在我只看到Wordpress网站标题

But right now I just see the Wordpress site title

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

Do I have to "activate" or "initiate" the function **logo_exists? or is there another problem?**

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

谢谢!

推荐答案

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

$options doesn't exist in the scope of that function. You need to add

global $options;

紧接在函数声明之后,以便使用 $ 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">
    ...

这篇关于Wordpress的function.php文件中的PHP条件语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆