3

I'm using the ACF plugin for WordPress. I'm posting to a custom post type programmatically via my theme. Now, I thought I would be able to save an array of items to a custom field quite easily, but whenever I try to, nothing gets input. I simply get this error when I check the post screen in the admin area:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /home/lukeseag/public_html/spidr-wp/wp-content/plugins/advanced-custom-fields/core/fields/text.php on line 127

Now, I'm kind of throwing this out there for suggestions as to how I can go about solving this problem.

To give some context, I'm programmatically saving data input by a user, and creating a post with it. Much of the data is simple strings and numbers that can each have their own custom field.

But I have an unknown number of text strings and URL's (with an id number for each) coming through this page too, that need to be linked with the same post. I need to be able to output each set of text string and URL into their own div on the single.php post page. Now, ideally these text/url pairs would be saved in a single array so I can just loop through the array and output the data, but it looks like ACF doesn't want to let this happen?

Any suggestions as to how I can save this data and then output it easily on the single.php page? Each set of results will have an ID (number), text string and url.

Thanks!

4

2 回答 2

2

你可以使用 json_encode($your_array) 和 json_decode() 保存数组来恢复它。这也适用于关联数组。

于 2013-07-24T16:27:57.950 回答
2

这正是为什么所有这些"frameworks"通常都是more pain than gain. 它们被设计成对懒惰的人来说看起来很灵活,但在实际情况下它们总是被证明是无用的,这比没有它们实际上可以轻松实现的任何事情都要复杂一些。

无论如何,至于你的问题。

问题是您传递的是 array 而不是 string 。我不打算调试插件,无论如何,需要更多的代码和信息,所以我只会给你一个可能和简单的解决方案,那就是编写一个pseudo-array string这样的:

'ID|URL|STRING'

请注意,分隔符管道 (|) 只是一个示例,您可以使用 ( ,) ( :) ( #) 或其他任何东西。

换句话说 :

$url = 'http://myurl.url/something';
$id = 35;
$string = 'my string';

$pseudo_r = $id . '|' . $url .  '|' . $string . '|'; // results in 'ID|URL|STRING';

或者您可以使用该implode()功能

 $pseudo_r = array(
       "url" => $url,
       "id" => $id ,
       "string" => $string
       );

$pseudo_r = implode("|", $pseudo_r); // gives a string

并将其作为字符串传递,稍后您可以explode() 像这样分解它:

$pseudo_r = explode( '|', $pseudo_r ); // gives back the original array

(请注意,现在数组NON ASSOCIATIVE不再存在。)

现在,由于您提到其中每个都是 a set,因此您可以使用不同的分隔符从集合中组合相同的伪数组。

这可能对你有用,但是essentially it is wrong

为什么错了?

因为这是一个通用的且有点骇人听闻的解决方案,实际上忽略了您的数据类型。您正在混合 3 种类型的数据类型URLID和 a STRING。所有 3(应该)都有可能不同的验证方案,我怀疑这htmlspecialchars()部分属于URL.

另一种解决方案是在 ACF 设置中找到正确的数据字段,或者为每个字段使用不同的字段,然后将其一起显示,同样使用implode()explode()函数。

于 2013-04-27T01:55:24.813 回答