dqzve68846 2014-03-23 07:57
浏览 45

数组在wordpress中的含义

So I am working with wordpress and using the following code I get the following. I know this must be a basic question but I want to know what it means:

[1012] => Array ( [0] => 1 ) [1013] => Array ( [0] => 0 ) [1014] => Array ( [0] => 0 ) [1018] => Array ( [0] => 0 )

PHP:

<?php
 $all_meta_for_user = get_user_meta(get_current_user_id());
 print_r( $all_meta_for_user );
?>
  • 写回答

1条回答 默认 最新

  • dongzhuner6981 2014-03-23 08:06
    关注

    This data is user's metadata https://codex.wordpress.org/Function_Reference/get_user_meta

    As you say, this is your metadata. Two things here: first of all, I'll store all that data together inside a single key and I'll remove the uneeded single-element array:

    [my_plugin] => Array( [1012] => 1, [1013] => 0 ...)

    That lowers the possibility of your data mixing with other's plugin data, having conflicts, etc. Also, as second array is probably not needed, it will make access to it a little simpler.

    When you have that, it's only a matter of accessing the value like this:

    if ($all_meta_for_user['my_plugin'][$id] == 1) show_the_post()

    where $id is the post ID.

    To use a single key, I'll do something like this (untested):

    $posts_meta_for_user = get_user_meta(get_current_user_id(), 'my_plugin', true); if (is_array($posts_meta_for_user)) { $posts_meta_for_user[$new_id] = $new_value; update_user_meta(get_current_user_id(), 'my_plugin', $posts_meta_for_user); } else { $posts_meta_for_user = array($new_id => $new_value); add_user_meta(get_current_user_id(), 'my_plugin', $awesome_level); }

    Notice that we get only the meta value named 'my_plugin' and test it already had a value by checking that is an array. If it is, we update it, and if it's not, we create a new one. $new_id is the post ID you want to store and $new_value the value.

    评论

报告相同问题?

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000