开发者

Appending arrays in PHP

I have three arrays which represent category IDs in Wordpress, all with the format $base_cat_id['term_id']. What I want to do is assign a post to three of these categories using the following 开发者_如何学编程function:

wp_set_post_categories($entry['post_id'], $base_cat_id + $generic_n_cat_id + $specific_n_cat_id);

However, when I do this the post is only assigned to the first two categories. Am I using the right method to add these arrays together?

Edit:

I got this to work by doing the following:

$cat_ids = array($base_cat_id['term_id'], $generic_a_cat_id['term_id'], $specific_a_cat_id['term_id']);
wp_set_post_categories($entry['post_id'], $cat_ids);

It's not pretty. But I found that using array_merge with the same string ID does not work, as it overwrites the values. Union does not work either as I can only use a union for two arrays. Please let me know if there is a better way!


Use the function array_merge(). Its arguments are as many arrays as you are merging and it returns those arrays merged in one array, so it returns an array.

like this:

wp_set_post_categories($entry['post_id'], array_merge($base_cat_id, $generic_n_cat_id, $specific_n_cat_id));

Caveat: your if your arrays are multilevel, then you might get weird results. For more info check out: http://php.net/manual/en/function.array-merge.php


I'm not quite sure what's in your array other that 'term_id', but try using this:

wp_set_post_categories($entry['post_id'], array_merge($base_cat_id, $generic_n_cat_id, $specific_n_cat_id));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜