返回一个会话变量中的所有自定义字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了返回一个会话变量中的所有自定义字段相关的知识,希望对你有一定的参考价值。

Returns all of the custom fields (meta data) from a page / post and assigns them all to a single session array for compactness
  1. /**
  2. * Usage: Add all_my_customs(); to your page somewhere (header.php is best)
  3. * Usage: if you would like to get the postmeta from a specific page use, all_my_customs('7');
  4. * To access the value <?php echo $_SESSION['pc']['your_custom_name']; ?>
  5. */
  6.  
  7. function all_my_customs($id = 0){
  8.  
  9. //if a post id is not provided grab the page currently being presented
  10. if ($id == 0)
  11. {
  12. global $wp_query;
  13. $content_array = $wp_query->get_queried_object();
  14. $id = $content_array->ID;
  15. }
  16.  
  17. //grab the customs in their raw form from wp
  18. $raw = get_post_custom($id);
  19. //if the raw data is an array run a loop
  20. if (is_array($raw))
  21. {
  22. foreach($raw as $key => $value)
  23. {
  24. //if the number of items returned for the cf is greater than one (an array of items)
  25. if (count($value) > 1)
  26. {
  27. //retain all
  28. foreach($value as $v)
  29. {
  30. $return[$key][] = $v;
  31. }
  32. }
  33. //else add the first item
  34. else
  35. {
  36. $return[$key] = $value[0];
  37. }
  38. }
  39.  
  40. }
  41. //and returns the session array.
  42. $_SESSION['pc'] = $return;
  43. return $_SESSION['pc'];
  44. }

以上是关于返回一个会话变量中的所有自定义字段的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 自定义表单记住文本字段数据

Mysql数据库中的系统变量和自定义变量的使用步骤和语法

如何在自定义 woocommerce 结帐字段中显示 $_SESSION 变量?

如何在 Django 会话模型中设置自定义字段?

在 IN() 函数中使用用户定义的会话变量返回意外结果

如何在 graphQL 片段中定义可选字段以进行查询