PHP 配列要素内の文字列を連結するにはどうすればよいですか?
-
21-09-2019 - |
質問
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
.
解決
bloginfo
('stylesheet_directory') はスタイルシート ディレクトリをエコーします。配列を宣言すると、実質的に stdout に書き込むことになります。これがページの上部に表示される理由です。あなたが探しているのは get_bloginfo
.
他のヒント
使用破するます:
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
のり文字列の配列要素に参加。
最後にカンマが入っているようです。それかもしれません。それを取り外してテストします。また、\" を単一の ' に置き換えました。
アップデート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);
}
私は、これはあなたの質問にの技術的の答えではないことを知っていますが、考えられています:
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関数呼び出しを作る伴うだろう。
所属していません StackOverflow