2012-09-19 43 views
0

如果在Wordpress主題中有條件,我想創建一個不太笨拙的版本。我試圖找出如何做到這一點而無需打開和關閉的樣式標籤兩次:PHP if語句「if X and/or Y」

<?php if (get_theme_mod('main_color')) : ?> 
    <style> 
    #branding { 
     background: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
    } 
    a:link { 
     color: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
    } 
    </style> 
    <?php endif ?> 
    <?php if (get_theme_mod('links_color')) : ?> 
    <style> 
    #wrapper a:link { 
     color: <?php echo get_theme_mod('links_color', '#6C84B4')."\n"; ?>; 
    } 
    </style> 
    <?php endif ?> 

我想這兩個具有以下組合:

<?php if (get_theme_mod('main_color' || 'links_color')) : ?> 
     <style> 
     #branding { 
      background: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
     } 
     a:link { 
      color: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
     } 
     #wrapper a:link { 
      color: <?php echo get_theme_mod('links_color', '#6C84B4')."\n"; ?>; 
     } 
     </style> 
     <?php endif ?> 

但出於某種原因,這沒有按」工作。

任何幫助?

回答

0

上面的答案很好,但我認爲他們錯過了一些東西。

遺囑正本代碼:

  • 添加某些風格,如果條件A被滿足。
  • 如果滿足條件B,它將添加其他樣式。

,結合它們是這樣的:

<?php if (get_theme_mod('main_color') || get_theme_mod('links_color')) : ?> 

修復了OP的企圖的錯誤,但OP的嘗試沒有到原來的簡單的打印"less style tags"目標的精確解。這是因爲如果滿足這些條件中的任何一個,那麼將應用兩種樣式。

(也許這是所期望的功能?)

Geralds答案是接近,因爲它堅持原來的邏輯,但它總是會打印STYLE標籤,如果不考慮它的滿足。鑑於原始海報希望「清理HTML」,只有在需要打印樣式的情況下,發佈STYLE標籤仍然可以有更好的解決方案。

的解決辦法是在PHP中執行所有的邏輯,在HTML代碼添加到一個緩衝區,然後打印緩衝區,如果它不是空的。你可以通過使用實際的緩衝區ob_start()或者將字符串保存到一個變量中。我會用更直接的「保存到變量」的方法。

<?php 
// initialize the buffer we'll save our style strings too 
$buffer = ''; 
// save the styles to the buffer for main 
if (get_theme_mod('main_color')) { 
    $buffer .= 
    "#branding { 
     background: " . get_theme_mod('main_color', '#243964') . "\n 
    } 
    a:link { 
     color: " . get_theme_mod('main_color', '#243964') . "\n 
    }\n"; 
} 
// save the styles to the buffer for link colors 
if (get_theme_mod('links_color')) { 
    $buffer .= 
    "#wrapper a:link { 
      color: " . get_theme_mod('links_color', '#6C84B4') . "\n 
    }\n"; 
} 

// verify there's styles to print and print them into a single STYLE tag 
if(!$buffer) { 
    echo "<style>" . $buffer . "</style>"; 
} 

>

這將:

  1. 保持同樣的邏輯,當額外的樣式添加
  2. 方面只打印一個STYLE標籤
  3. 只有打印STYLE標籤如果有款式可以打印

我希望有幫助!

+0

太棒了!謝謝! – user18577

1

與嘗試:

<?php if (get_theme_mod('main_color') || get_theme_mod('links_color')) : ?> 
1
<?php if (get_theme_mod('main_color' || 'links_color')) : ?> 

應該

<?php if (get_theme_mod('main_color') || get_theme_mod('links_color'))) : ?> 

因爲get_theme_mod()是你向它傳遞參數的函數。你可以改變函數接受幾個paramiters,那麼你可以調用的功能等:

<?php 
function get_theme_mod($main_color, $links_color) { 
    // do stuff and return bool 
} 

if (get_theme_mod('main_color', 'links_color')) : 
?> 

而且這不是主要的問題,你想做些什麼,但你應該終止<?php endif; ?>

1

看完後你再次發帖,我想這是你試圖達到什麼:

<style> 
<?php if (get_theme_mod('main_color')) : ?> 
    #branding { 
     background: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
    } 
    a:link { 
     color: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
    } 
<?php endif ?> 
<?php if (get_theme_mod('links_color')) : ?> 
    #wrapper a:link { 
     color: <?php echo get_theme_mod('links_color', '#6C84B4')."\n"; ?>; 
    } 
<?php endif ?> 

</style> 

這將只顯示想要的CSS代碼,根據不同的主題的選擇,但你只會有一個<style>標籤打開和關閉。

0
<?php if (get_theme_mod('main_color') || get_theme_mod('links_color')) : ?> 
<style type="text/css"> 
<?php endif ?> 
<?php if (get_theme_mod('main_color')) : ?> 
#branding { 
    background: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
} 
a:link { 
    color: <?php echo get_theme_mod('main_color', '#243964')."\n"; ?>; 
} 
<?php endif ?> 
<?php if (get_theme_mod('links_color')) : ?> 
#wrapper a:link { 
    color: <?php echo get_theme_mod('links_color', '#6C84B4')."\n"; ?>; 
} 
<?php endif ?> 

<?php if (get_theme_mod('main_color') || get_theme_mod('links_color')) : ?> 
</style> 
<?php endif ?>