【问题标题】:Use php to identify duplicate settings使用php识别重复设置
【发布时间】:2015-09-05 08:34:22
【问题描述】:

我正在构建一个 wordpress 主题,在主题定制器中,您可以为正文、标题和导航栏选择 Google 字体。

下面显示了我当前如何将选择的字体导入到在我的页面头部调用的 custom-css.php 文件中:

$base_font = get_theme_mod('font_base_family');
$headers_font = get_theme_mod('font_headings_family');
$menu_font = get_theme_mod('font_nav_family');

if($base_font !='') { ?>
<link href='http://fonts.googleapis.com/css?family=<?php echo str_replace(" ","+",$base_font );?>:100,200,300,400,500,600,700,800,900' rel='stylesheet' type='text/css'>
<?php } ?> 

<?php if ($headers_font != '') { ?>
<link href='http://fonts.googleapis.com/css?family=<?php echo str_replace(" ","+",$headers_font );?>:100,200,300,400,500,600,700,800,900' rel='stylesheet' type='text/css'>
<?php } ?>

<?php if ($menu_font != '') { ?>
<link href='http://fonts.googleapis.com/css?family=<?php echo str_replace(" ","+",$menu_font );?>:100,200,300,400,500,600,700,800,900' rel='stylesheet' type='text/css'>
<?php } ?>

然后在我的 custom-css.php 文件中我有这样的东西(缩短):

body {
font-family: <?php echo get_theme_mod('font_base_family'); ?>;
}
h1, h2, h3, h4, h5, h6 {
font-family: <?php echo get_theme_mod('font_headings_family'); ?>;
}
#header nav ul li > a, #header nav ul li > span {
font-family: <?php echo get_theme_mod('font_nav_family'); ?>;
}

我遇到的问题显然是加载 3 种这样的字体以及所有字体权重,这会大大增加页面加载时间。如果用户想要每种字体使用 3 种不同的字体,那很好,但是如果说他们决定使用 2 或 3 种相同的字体,我不想在脑海中重复。

比如说,有人想要正文和标题使用相同的字体,有没有办法我可以使用 php 说“如果 a 与 b 或 c 相同 - 删除重复项”

希望这是有道理的。

【问题讨论】:

  • 将字体分配给一个数组,数组key是字体名称,如果有重复,你会用相同的东西覆盖键,这意味着如果所有3个都相同,仍然会有是数组中的 1 个键。循环(foreach)数组并在循环内输出&lt;link&gt;标签。

标签: php css wordpress fonts


【解决方案1】:

将字体分配给数组键并对其进行迭代。

$fonts = array(
    get_theme_mod('font_base_family') => 'base',
    get_theme_mod('font_headings_family') => 'headings',
    get_theme_mod('font_nav_family') => 'nav'
);

foreach ($fonts as $key => $type) { ?>
    <link href='http://fonts.googleapis.com/css?family=<?php echo str_replace(" ", "+", $key); ?>:100,200,300,400,500,600,700,800,900' rel='stylesheet' type='text/css'>
<?php }

【讨论】:

    猜你喜欢
    • 2018-08-23
    • 1970-01-01
    • 2017-04-14
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2018-06-15
    相关资源
    最近更新 更多