新!
好吧我删除了bg选项,但现在我从font-family中得到错误..
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 20
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 26
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 31
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 37
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 42
Warning: Illegal string offset 'font-family' in .../wp-content/themes/curves/includes/custom-css.php on line 47
Warning: Illegal string offset 'font-family' in ..../wp-content/themes/curves/includes/custom-css.php on line 56
我认为这不好写
$h1_font = ot_get_option( 'h1_font', 'lato' );
$custom_css = "
h1 {
font-family: ". $h1_font['font-family'] .";
font-size: ". ot_get_option( 'h1_size', '24' ). "px;
font-weight: 300;
}
任何帮助? THX
PHP 5.4有一些问题..我有一个为我编写代码的朋友,因为我只是设计,现在这个人无法访问...我在这里:)
所以我有customcss.php
为Wordpress主题创建dynamic.css
。
我已经在主机gator上安装了主题为PHP 5.4并且我得到了这个警告:
Warning: Illegal string offset 'font-family' in /.../public_html/aaa/wp- content/themes/curves/includes/custom-css.php on line 60
Notice: Uninitialized string offset: 0 in /.../public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 20
Warning: Illegal string offset 'background-color' in /home4/mthemes/public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 60
Notice: Uninitialized string offset: 0 in /.../public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 60
Warning: Illegal string offset 'background-repeat' in /.../public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 63
Notice: Uninitialized string offset: 0 in /.../public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 63
Warning: Illegal string offset 'background-attachment' in /.../public_html/aaa/wp-content/themes/curves/includes/custom-css.php on line 67
等。
<?php
//custom inline css
function curves_style_custom() {
wp_enqueue_style(
'custom-style',
get_template_directory_uri() . '/dynamic.css'
);
$h1_font = ot_get_option( 'h1_font', 'lato' );
$h2_font = ot_get_option( 'h2_font', 'lato' );
$h3_font = ot_get_option( 'h3_font', 'lato' );
$h4_font = ot_get_option( 'h4_font', 'lato' );
$h5_font = ot_get_option( 'h5_font', 'lato' );
$h6_font = ot_get_option( 'h6_font', 'lato' );
$g_font = ot_get_option( 'g_font', 'lato' );
$custom_css = "
h1 {
font-family: ". $h1_font['font-family'] .";
font-size: ". ot_get_option( 'h1_size', '24' ). "px;
font-weight: 300;
}
h2 {
font-family: ". $h2_font['font-family'] .";
font-size: ". ot_get_option( 'h2_size', '22' ) ."px;
font-weight: 300;
}
h3 {
font-family: ". $h3_font['font-family'] .";
font-size:". ot_get_option( 'h3_size', '18' ) ."px;
font-weight: 300;
}
h4 {
font-family: ". $h4_font['font-family'] .";
font-size: ". ot_get_option( 'h4_size', '16' ) ."px;
font-weight: 300;
}
h5 {
font-family: ". $h5_font['font-family'] .";
font-size: ". ot_get_option( 'h5_size', '12' ) ."px;
font-weight: 300;
}
h6 {
font-family: ". $h6_font['font-family'] .";
font-size: ". ot_get_option( 'h6_size', '10' ) ."px;
font-weight: 300;
}
p {
font-size: ". ot_get_option( 'p_size', '14' ) ."px;
}
body{
font-family: ". $g_font['font-family'] .";
";
$body_bg = ot_get_option( 'body_bg_image' );
if($body_bg['background-color'] != ""){
$custom_css .= 'background-color: '. $body_bg['background-color'] .';';
}
if($body_bg['background-repeat'] != ""){
$custom_css .= 'background-repeat: '. $body_bg['background-repeat'] .';';
}
if($body_bg['background-attachment'] != ""){
$custom_css .= 'background-attachment: '. $body_bg['background-attachment'] .';';
}
if($body_bg['background-position'] != ""){
$custom_css .= 'background-position: '. $body_bg['background-position'] .';';
}
if($body_bg['background-size'] != ""){
$custom_css .= 'background-size: '. $body_bg['background-size'] .';';
}
if($body_bg['background-image'] != ""){
$custom_css .= 'background-image: url("'. $body_bg['background-image'] .'");';
}
$custom_css .= "}";
此代码将在admin(选项树)中选择的选项应用于内联css。
在PHP 5.3中它可以工作,但在PHP 5.4中它会显示警告。
此外,我发现发生这种情况是因为,当我第一次安装主题时,在选项树中没有选择font-family
这样的选项,尽管您可以看到我设置了默认值。
答案 0 :(得分:1)
在使用数组成员之前,首先应检查是否通过例如:
进行设置if(isset($body_bg['background-image'])) {
// do something with it
}
另见Illegal string offset Warning PHP
中的答案例如,您可以执行以下操作:
$h1_font = ot_get_option( 'h1_font', 'lato' );
$h1_font_family = "sans-serif";
if(isset($h1_font['font-family']))
$h1_font_family = $h1_font['font-family'];
$custom_css = "
h1 {
font-family: ". $h1_font_family .";
font-size: ". ot_get_option( 'h1_size', '24' ). "px;
font-weight: 300;
}
等等,您想要定义的所有样式。您提供的是默认值("sans-serif"
),如果您的选项中的设置'font-family'
可用,则会替换默认值。