如果在Wordpress主题中有条件的话,我试图创建一个不那么笨重的版本。我试图找出如何在不打开和关闭样式标记两次的情况下执行此操作:
<?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 :(得分:1)
尝试:
<?php if (get_theme_mod( 'main_color') || get_theme_mod( 'links_color' )) : ?>
答案 1 :(得分: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()
是一个函数,所以您可以将参数传递给它。您可以更改函数以接受多个参数,然后您可以调用函数,如:
<?php
function get_theme_mod($main_color, $links_color) {
// do stuff and return bool
}
if (get_theme_mod( 'main_color', 'links_color' )) :
?>
这也不是您尝试做的主要问题,但您应该终止<?php endif; ?>
答案 2 :(得分: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>
代码。
答案 3 :(得分: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 ?>
答案 4 :(得分:0)
上述答案很好,但我认为他们遗漏了一些东西。
原始代码将:
将它们组合起来:
<?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>";
}
&GT;
这将:
STYLE
代码STYLE
标记我希望有所帮助!