【问题标题】:PHP conditional statements within Wordpress's function.php file not workingWordpress 的 function.php 文件中的 PHP 条件语句不起作用
【发布时间】:2011-05-27 19:21:52
【问题描述】:

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

我对 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 是在 &lt;style&gt; 标签之前初始化的。)

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

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

(所以图像工作)

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

我必须“激活”或“启动”函数**logo_exists吗? 还是有其他问题?**

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

提前致谢!

【问题讨论】:

  • 我建议使用 if..endif 语法重新格式化您的代码,以提高可读性(在混合 PHP/HTML 代码中)。
  • BlotClock 仍然有一点,$options 变量在你的函数内没有作用域,如果你想在该函数内引用该变量,你需要在函数内将其全球化。我认为,问题在于您的函数需要挂钩到适当的操作,例如add_action( 'wp_print_styles', 'logo_exists' ) 以将内容输出到标题中的适当位置以进行样式设置。

标签: php wordpress conditional-statements


【解决方案1】:

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

global $options;

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

function logo_exists() {
    global $options;

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

【讨论】:

  • 对不起,我试过了,但还是不行(仍然看到 #header h1 a 的文本。我不确定它是否有用,但 plugin_options 是在
猜你喜欢
  • 2011-05-29
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-01
相关资源
最近更新 更多