2010-12-19 144 views
0

我想在function.php範圍內執行以下PHP語句:如果上傳的圖像爲#header h1 a的背景,請縮進文本(刪除)...如果不是(else) ,保留文字(不要縮進)。在Wordpress的function.php文件中PHP條件語句不起作用

我不是很熟悉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在<style>標籤之前被初始化。)

我做了var_dump#options['logo'] dis飾演:

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

(所以圖像工作)

但是現在我只看到WordPress網站標題

我必須「激活」或「開始」功能** logo_exists? 或者是有其他問題?**

(一切工作正常,如果我不使用PHP如果聲明,並宣佈$選擇[徽標]直接從header.php文件)提前

謝謝!

+0

我建議重新格式化你的代碼[if..endif(http://php.net/manual/en/control-structures.alternative-syntax.php)語法變得更加可讀(以混合的PHP/HTML代碼)。 – 2010-12-19 09:30:47

+0

儘管BlotClock還有一點,但$ options變量在函數內部沒有作用域,如果你想引用那個函數內部的變量,你需要在函數內全局化它。我認爲,問題在於你的函數需要掛鉤到合適的動作上,例如'add_action('wp_print_styles','logo_exists')'將內容輸出到頭部的適當位置以進行造型。 – t31os 2010-12-19 11:18:14

回答

3

$options在該函數的範圍內不存在。您需要添加

global $options; 

只是在函數聲明之後,這樣它拿起$options變量從包括。就像這樣:

function logo_exists() { 
    global $options; 

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

對不起,我試過,但仍然沒有工作(仍然看到#header h1的文本a。我不確定它是否是有用的信息,但plugin_options是在