返回一个会话变量中的所有自定义字段
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
/** * Usage: Add all_my_customs(); to your page somewhere (header.php is best) * Usage: if you would like to get the postmeta from a specific page use, all_my_customs('7'); * To access the value <?php echo $_SESSION['pc']['your_custom_name']; ?> */ function all_my_customs($id = 0){ //if a post id is not provided grab the page currently being presented if ($id == 0) { global $wp_query; $content_array = $wp_query->get_queried_object(); $id = $content_array->ID; } //grab the customs in their raw form from wp $raw = get_post_custom($id); //if the raw data is an array run a loop if (is_array($raw)) { foreach($raw as $key => $value) { //if the number of items returned for the cf is greater than one (an array of items) if (count($value) > 1) { //retain all foreach($value as $v) { $return[$key][] = $v; } } //else add the first item else { $return[$key] = $value[0]; } } } //and returns the session array. $_SESSION['pc'] = $return; return $_SESSION['pc']; }
以上是关于返回一个会话变量中的所有自定义字段的主要内容,如果未能解决你的问题,请参考以下文章