English 中文(简体)
How do I concatenate a string in a PHP Array Element?
原标题:

I am customizing wordpress blog and I have a need to make custom sidebar widgets. My PHP is rusty at best. What I am trying to do is concatenate a php variable into a string being set as an array element. here is the code I am using, it doesn t seem to work. All it does is print the stylesheet directory at the top of every page:

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>",
    ));

so as you can see here I am trying to concatenate the bloginfo( stylesheet_directory ) into 2 of the elements. This doesn t work properly. It just ends up printing it at the top of the page before the doctype.

最佳回答

bloginfo( stylesheet_directory ) will echo the stylesheet directory. When you declare the array, you are effectively writing to stdout. This is why it will show on top of the page. What you are looking for is get_bloginfo.

问题回答

Use implode:

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

Join array elements with a glue string.

It looks like you have a comma at the end. It might be that. Remove it and test. I ve also replace " with a singe .

UPDATE replaced bloginfo() with 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);
}

I know that this is not technically the answer to your question, but have you considered:

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>",
    ));

It would be easier and faster -- it would only involve making the bloginfo function call once.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签