代码之家  ›  专栏  ›  技术社区  ›  Russ Bradberry

如何在php数组元素中连接字符串?

  •  1
  • Russ Bradberry  · 技术社区  · 15 年前

    我正在定制WordPress博客,我需要定制侧边栏小部件。我的PHP最好是生锈的。我要做的是将一个PHP变量连接到一个字符串中,该字符串被设置为数组元素。这是我正在使用的代码,它似乎不起作用。它所做的就是在每一页的顶部打印样式表目录:

    if ( function_exists("register_sidebar") )
        register_sidebar(array(
            "before_widget" => "<div class=\"rounded_box\"><div class=\"top_curve\"><img src=\"".bloginfo('stylesheet_directory')."/images/top_curve.jpg\" alt=\"Top\" width=\"247\" height=\"9\" /></div><div class=\"middle\">",
            "after_widget" => "</div><div class=\"bottom_curve\"><img src=\"".bloginfo('stylesheet_directory')."/images/bottom_curve.jpg\" alt=\"Bottom\"  /></div></div>",
            "before_title" => "<h2>",
            "after_title" => "</h2>",
        ));
    

    如您所见,我正在尝试连接 bloginfo('stylesheet_directory') 变成2个元素。这个不能正常工作。它最后只会打印在页面顶部, doctype .

    4 回复  |  直到 8 年前
        1
  •  3
  •   Gordon Haim Evgi    15 年前

    bloginfo ('stylesheet_directory')将回送样式表目录。当您声明数组时,实际上是在写入stdout。这就是它将显示在页面顶部的原因。你要找的是 get_bloginfo .

        2
  •  0
  •   bluebrother    15 年前

    使用 implode :

    string implode  ( string $glue  , array $pieces  )
    string implode ( array $pieces )
    

    用粘合字符串连接数组元素。

        3
  •  0
  •   Steven    15 年前

    看起来结尾有个逗号。可能就是这样。拆下并测试。 我也用单曲代替了。

    更新 已将bloginfo()替换为get_bloginfo()。

    if ( function_exists("register_sidebar") )
    {
      $args =array(
      "before_widget" => "<div class='rounded_box'><div class='top_curve'><img src='".get_bloginfo('stylesheet_directory')."/images/top_curve.jpg' alt='Top' width='247' height='9' /></div><div class='middle'>",
      "after_widget" => "</div><div class='bottom_curve'><img src='".get_bloginfo('stylesheet_directory')."/images/bottom_curve.jpg' alt='Bottom' /></div></div>",
      "before_title" => "<h2>",
      "after_title" => "</h2>");'
    
      register_sidebar($args);
    }
    
        4
  •  0
  •   cwallenpoole    15 年前

    我知道这不是 技术上 回答你的问题,但你是否考虑过:

    if ( function_exists("register_sidebar") )
        $ssheet_dir = bloginfo('stylesheet_directory');
        register_sidebar(array(
                "before_widget" => "<div class=\"rounded_box\"><div class=\"top_curve\"><img src=\"$ssheet_dir/images/top_curve.jpg\" alt=\"Top\" width=\"247\" height=\"9\" /></div><div class=\"middle\">",
                "after_widget" => "</div><div class=\"bottom_curve\"><img src=\"$ssheet_dir/images/bottom_curve.jpg\" alt=\"Bottom\"  /></div></div>",
                "before_title" => "<h2>",
                "after_title" => "</h2>",
        ));
    

    它将变得越来越简单和快速——它只涉及调用一次bloginfo函数。

    推荐文章